Skip to content

namespace set values are getting lost in http socket close callback  #145

Description

@queston02

We are creating the namespace and setting a variable called gtid in one of the middle ware and then trying to read that value for logging. We are getting the values in most of the places but some places it's undefined. One of the place is in the callback of the socket close event. Below is the code to provide some context.

var express = require('express');
var app = express();
var http = require('http');
var continuationLocalStorage = require('continuation-local-storage');
var namespace = continuationLocalStorage.createNamespace('namespace');

app.use(function (req, res, next) {
    namespace.run(function () {
        namespace.set('gtid', 'some unique value');
        return next();
    });
});

var req = protocol.request(request, function (response) {
    response.setEncoding('utf-8');
    var responseBody = '';

    response.on('data', function (data) {
        responseBody += data;
    });

    response.on('error', function (e) {
        e.code = response.statusCode;
        return callback(e);
    });

    response.on('end', function () {
        return callback(null, responseBody);
    });
});

req.on('socket', function (socket) {
    if (!socket.name) {
        socket.name = new Date().getTime();

        socket.on('close', function () {
            var namespace = getNamespace('namespace');
            console.log(namespace.get('gtid'))
        });
    }
});

req.on('error', function (e) {
    return callback(e);
});

if (request.body) {
    req.write(request.body);
}
req.end();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions