loopback-context icon indicating copy to clipboard operation
loopback-context copied to clipboard

No context available when using express-session with Memory store

Open azatoth opened this issue 10 years ago • 12 comments

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

azatoth avatar Aug 17 '16 07:08 azatoth

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.

azatoth avatar Aug 17 '16 07:08 azatoth

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.

bajtos avatar Aug 17 '16 08:08 bajtos

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.

bajtos avatar Aug 17 '16 11:08 bajtos

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 avatar Aug 17 '16 11:08 bajtos

@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 avatar Aug 19 '16 00:08 Jeff-Lewis

@Jeff-Lewis initially I was testing with cls-hooked enabled (see https://github.com/azatoth/loopback-sandbox)

azatoth avatar Aug 19 '16 08:08 azatoth

@Jeff-Lewis there is a pending pull request reworking loopback-context to use cls-hooked, see https://github.com/strongloop/loopback-context/pull/2

bajtos avatar Aug 24 '16 12:08 bajtos

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

josieusa avatar Aug 24 '16 13:08 josieusa

Link to PR #11

josieusa avatar Dec 28 '16 09:12 josieusa

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.

josieusa avatar Jan 03 '17 15:01 josieusa

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.

stale[bot] avatar Feb 11 '20 03:02 stale[bot]

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.

stale[bot] avatar Feb 27 '20 05:02 stale[bot]