Skip to content

Active Domain context is being leaked between server requests. #26081

@m0uneer

Description

@m0uneer

Version: v6.14.3 and v10.5.0
Express Version: 4.16.4
Platform:
Linux m-pc 4.13.0-41-generic # 46~16.04.1-Ubuntu SMP Thu May 3 10:06:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Steps (Generating a simple Express app):

  • $ npm install express-generator -g
  • $ express --view=pug myapp
  • $ npm i
  • Removing unneeded dependencies to keep it as simple and free of external dependencies.
  • Adding some domain implementations.
var domain = require('domain');

// Patching global promise in response to https://github.com/nodejs/node-v0.x-archive/issues/8648
class PatchedPromise extends Promise { 
  // ...
}

Promise = PatchedPromise;

// Adding the `__awaiter` method to support old nodejs versions
var __awaiter = function (thisArg, _arguments, P, generator) {
  // ...
};

var express = require('express');
var app = express();

app.use(function(req, res, next) {
  let activeDomain = domain.active ? domain.active : domain.create();
  activeDomain.run(() => {
    if (domain.active.trap) {
      throw new Error('Context Leaked!');
    }

    domain.active.trap = {};
    __awaiter(this, void 0, void 0, function* () {
      try {
        yield new Promise(() => {
          throw new Error('test');
        });
      }
      catch (err) {
        next(err);
      }
    });
  });
});

module.exports = app;
  • $ npm start
  • Send a POST request to /.

Expected:
Always return Error('test').

Actual:
Only first request returns Error('test') and all the comming requests return Error('Context Leaked!')

A link to a repo to reproduce:
https://github.com/m0uneer/domain-promise-problem

Metadata

Metadata

Assignees

No one assigned

    Labels

    domainIssues and PRs related to the domain subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions