Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Uncaught exception triggered by response.end() #1223

Closed
veita opened this issue Jun 24, 2011 · 2 comments
Closed

Uncaught exception triggered by response.end() #1223

veita opened this issue Jun 24, 2011 · 2 comments
Labels

Comments

@veita
Copy link

veita commented Jun 24, 2011

With node 0.4.8 HTTP the attached HTTP server always produces the "TypeError: Cannot read property '_httpMessage' of null".

The server delivers HTML documents from its current working directory.

var
    g_sys  = require('sys'),
    g_http = require('http'),
    g_url  = require('url'),
    g_path = require('path'),
    g_fs   = require('fs'),

    log = console.log,

    staticFile = function(res, path, ct) {
        g_fs.stat(path, function(err, stats) {
            res.writeHead(200, 'OK', {'Content-Type': ct, 'Content-Length': stats.size});

            g_sys.pump(g_fs.createReadStream(path), res, function() {res.end();}); // error
            //g_sys.pump(g_fs.createReadStream(path), res); // no error
        });
    },

    main = function(req, res) {
        var
            url     = g_url.parse(req.url),
            strPath = g_path.join(process.cwd(), url.pathname);

        log("Path: " + strPath);

        staticFile(res, strPath, 'text/html');
    };


process.on('uncaughtException', function(err) {
    log('Uncaught exception:\n' + err);
});

process.on('SIGINT', function() {
    log('bye');
    process.exit(0);
});

g_http.createServer(main).listen(1337, '127.0.0.1');
@koichik
Copy link

koichik commented Jun 24, 2011

util.pump() calls end() of writable stream.
https://github.com/joyent/node/blob/v0.4/lib/util.js#L391

I think end() was called twice in error case.

@veita
Copy link
Author

veita commented Jun 24, 2011

You're right. My fault. end() is called twice, even if no error occurred.

But shouldn't the error message be more helpful? E.g. 'end() called for already closed object' or something like that. Another solution would be to let end() be idempotent, but I'm not sure if this is a better approach since it is prone to hide programming errors.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants