No context available when using express-session with Memory store
If using session and token middleware, trying to access the explorer crashes the server with a "Error: No context available. ns.run() or ns.bind() must be called first."
see also strongloop/loopback#2636 and https://github.com/azatoth/loopback-sandbox for a testcase (is also reproducible if using plain loopback-context 1.0.0 without the cls-hooked patch)
@bajtos
As loopback is not using getCurrentContext() anymore but is accessing the raw loopbackContext object, there is no check if the context is active or not.
So apparently the getCurrentContext checks for ns.active too. The patch https://github.com/strongloop/loopback/pull/2604 which inlined getCurrentContext did not include this check.
The problem is caused by the memory store provided by express-session, which calls setImmediate to defer callbacks - see session/memory.js#L25-L27. Apparently setImmediate is not supported by node-continuation-local-storage.
I agree loopback apps should not crash in such case, https://github.com/strongloop/loopback/pull/2649 will fix that.
However, fixing express-session is up to somebody else to do.
The problem is caused by the memory store provided by express-session, which calls setImmediate to defer callbacks - see session/memory.js#L25-L27. Apparently setImmediate is not supported by node-continuation-local-storage.
Let me clarify this. When I changed session/memory to use process.nextTick instead of setImmediate, then the app started to work. There may be something else in play, because node-continuation-local-storage claims to support setImmediate according to their docs (https://github.com/othiym23/node-continuation-local-storage#continuation-local-storage).
@bajtos have you had a chance to try cls-hooked? I'm curious if it helps with some of these known cls issues. The latest cls-hooked version supports node 4.5.0.
@Jeff-Lewis initially I was testing with cls-hooked enabled (see https://github.com/azatoth/loopback-sandbox)
@Jeff-Lewis there is a pending pull request reworking loopback-context to use cls-hooked, see https://github.com/strongloop/loopback-context/pull/2
cls-hooked depends on async-hook, which may supportsetImmediate, or at least so it seems from the source code. See https://github.com/AndreasMadsen/async-hook/blob/9c2d1ee927233ec1ad31cbf286029c1c98bd9885/patches/timers.js#L16
Link to PR #11
express-session is known to have problems with cls-hooked, see othiym23/node-continuation-local-storage#29. Maybe I could add tests for express-session to PR #19 to see if it solves the issue.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.