Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async local storage doesn't work with debuggers #3808

Closed
jaysson opened this issue Jul 23, 2022 · 21 comments
Closed

Async local storage doesn't work with debuggers #3808

jaysson opened this issue Jul 23, 2022 · 21 comments
Assignees
Labels
Type: Bug The issue has indentified a bug

Comments

@jaysson
Copy link

jaysson commented Jul 23, 2022

Package version

5.8.0

Node.js and npm version

Node: v18.6.0, NPM: 8.13.2

Sample Code (to reproduce the issue)

// config/app.ts
export const http = {
  // other config
  useAsyncLocalStorage: true,
}
// app/Services/MyService.ts

import HttpContext from '@ioc:Adonis/Core/HttpContext'

export default class MyService {
  public myFunction() {
    const context = HttpContext.get()
    console.log(context)
  }
}

It works fine when run normally. However context is null if I try to run it with yarn debug, vscode, or webstorm.

VSCode launch config

{
      "type": "node",
      "request": "launch",
      "name": "Debug Backend",
      "runtimeExecutable": "node",
      "runtimeArgs": ["ace", "serve", "--watch"],
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**", "${workspaceRoot}/node_modules/**/*"]
}
@quangthienit1994
Copy link

quangthienit1994 commented Aug 25, 2022

HttpContext working on each request, You can't use like that.

@jaysson
Copy link
Author

jaysson commented Aug 25, 2022

HttpContext working on each request

That's exactly what I am using it for.

You can't use like that.

Do you mean I can't use debuggers for async local storage? As in that's a node issue?

@quangthienit1994
Copy link

I don't know what is "yarn debug", but can you use "const context = HttpContext.get()" in controllers or not ?

@jaysson
Copy link
Author

jaysson commented Aug 25, 2022

I am able to const context = HttpContext.get() when a debugger is not attached to the node process, irrespective of whether it is a controller or a plain TS class. But I cannot use it if a debugger is attached to the process.

@quangthienit1994
Copy link

image
It's working, No problem. Please check your config and logic.

@jaysson
Copy link
Author

jaysson commented Aug 25, 2022

You did not run the debugger. Please do that. The issue is about whether it works in conjunction with debugger. Also, I am running it on MacOS if that helps.

@quangthienit1994
Copy link

quangthienit1994 commented Aug 25, 2022

{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "runtimeExecutable": "node", "runtimeArgs": ["ace", "serve"], "skipFiles": [ "<node_internals>/**" ], "program": "${file}" } ] }
It's running on debug of VS Code
image

@jaysson
Copy link
Author

jaysson commented Aug 25, 2022

I am afraid you are either still not working with the debugger when the request is processing or it just works on Windows. In any case, I am attaching a video of the issue demonstrating it on a fresh AdonisJS project.

https://www.youtube.com/watch?v=SBEwUXcQb1o

@thetutlage
Copy link
Member

thetutlage commented Aug 26, 2022

@jaysson The context you have imported inside your service (in the shared video) is from the incorrect path. Look at the usage section https://docs.adonisjs.com/guides/async-local-storage#usage

@thetutlage
Copy link
Member

Also, I am not sure how VSCode debugger works under the hood. What happens when you use the official chrome dev-tools debugger?

Run the following command to start the server in debugging mode.

node ace serve --watch --node-args="--inspect"

Here is Node.js documentation on debugging https://nodejs.org/en/docs/guides/debugging-getting-started/

@jaysson
Copy link
Author

jaysson commented Aug 26, 2022

Thank you for the response @thetutlage . While I did import the wrong path in the video, I had imported the right one earlier following the guide itself. node ace serve --watch --node-args="--inspect" also has the same problem.

Another video: https://youtu.be/_4ywWADRh1I

@thetutlage
Copy link
Member

Alright. Can you share this project as a repo and I will try to reproduce the issue on my end

@thetutlage
Copy link
Member

CC @targos Pinging in case you can share some insights on using debugger with Async local storage

@jaysson
Copy link
Author

jaysson commented Aug 26, 2022

Repo of the same code from the video. It has one route -> controller -> calls a plain TS class.

https://github.com/jaysson/adonis-als-debug

@thetutlage thetutlage self-assigned this Aug 26, 2022
@thetutlage
Copy link
Member

After going through this PR nodejs/node#13870. I can see that somewhere between 8.5.0 and the latest release of Node, the issue was fixed.

If you upgrade to the latest version of (ie 18.8.0 as of today) and then try with --inspect flag, then it should work

@jaysson
Copy link
Author

jaysson commented Aug 26, 2022

I am running node 18.6.0. Let me upgrade to 18.8.0 and see.

@jaysson
Copy link
Author

jaysson commented Aug 26, 2022

Same issue on 18.8.0 (installed from homebrew). I am still on macOS big sur if that matters.

@thetutlage
Copy link
Member

Yup, I can confirm the issue still exists. Have to decouple it from AdonisJS and see if it still exists and then report it with Node

@thetutlage thetutlage added the Type: Bug The issue has indentified a bug label Aug 26, 2022
@thetutlage
Copy link
Member

I can reproduce it outside of AdonisJS as well. Here is a sample project. https://github.com/thetutlage/node-als-debugging

Going to confirm with @targos before opening an issue

@thetutlage
Copy link
Member

Related issue nodejs/node#44415

@targos
Copy link
Member

targos commented Sep 1, 2022

This is fixed in Node.js and will be in the next release. In the mean time, you can use Node.js v16.x, which is not affected by the bug.

@targos targos closed this as completed Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue has indentified a bug
Projects
None yet
Development

No branches or pull requests

4 participants