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

fix: Log uncaught exceptions in worker thread handlers #1544

Merged
merged 1 commit into from
Aug 21, 2022

Conversation

aldenquimby
Copy link
Contributor

@aldenquimby aldenquimby commented Aug 16, 2022

Motivation and Context

How Has This Been Tested?

  • npm run lint passes locally
  • npm run test:unit - cannot get this to work locally. jest dependency appears to be missing?
  • Locally after this change, per the bug report, I now see:
✖ Example failure
✖ Error: Example failure
      at handler (/Users/alden/.../appHandler.js:27:11)
      at InProcessRunner.run (file:///Users/alden/.../node_modules/serverless-offline/src/lambda/handler-runner/in-process-runner/InProcessRunner.js:87:20)
      at async MessagePort.<anonymous> (file:///Users/alden/.../node_modules/serverless-offline/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js:25:14)
  • So I get both the underlying error message and the correct stack trace

@dnalborczyk
Copy link
Collaborator

thank you @aldenquimby !

regarding your first attempt, that might be a problem with logging and worker threads: nodejs/node#30491

I think it might be nice to see which handler (function key) actually caused the problem. I'll have a look at your first attempt and the log not showing up.

@aldenquimby
Copy link
Contributor Author

aldenquimby commented Aug 18, 2022

@dnalborczyk ok great, thank you. Another possible solution if you want to ensure the function name shows:

    } catch (e) {
      throw new RethrownError(`Uncaught error in '${this.#functionKey}' handler.`, e)
    }

And we'd need to create RethrownError:

class RethrownError extends Error {
  constructor(message, error) {
    super(message)
    this.name = this.constructor.name
    this.stack = error.stack
  }
}

That seems cleaner to me because then there's just one place errors are logged (in HttpServer.js), vs having console.error scattered throughout

Thoughts on this approach?

@dnalborczyk
Copy link
Collaborator

dnalborczyk commented Aug 20, 2022

@aldenquimby I try to find some time over the weekend to have a closer look. I like your ideas, I just want to see what exactly is wrong. I don't know crazy much about worker threads and the usage in the plugin is pretty much my first and only attempt.

just wanted to mention a couple things as food for thought:

result = handler(event, lambdaContext, callback)
the handler call you are referring to would only be caught in he catch block if the handler is a synchronous function and some unexpected unhandled error would occur, which would be only the case if someone would use callbacks or the deprecated context methods. the catch block would only be activated by accident, as synchronous handlers are not supported.

the second handler call

const callbackResult = await Promise.race(callbacks)
does not have a try/catch block, but probably should. this is for all asynchronous stuff (async function, returned Promise, callback (Promise wrapped), as well as context methods (Promise wrapped)).

@dnalborczyk dnalborczyk changed the title Log uncaught exceptions in worker thread handlers fix: Log uncaught exceptions in worker thread handlers Aug 21, 2022
@dnalborczyk dnalborczyk merged commit 498ce29 into dherault:master Aug 21, 2022
@dnalborczyk
Copy link
Collaborator

dnalborczyk commented Aug 21, 2022

@aldenquimby I pulled in your PR and added this: 06d348d . does this still cover your expectations?

one of the problems was that the serverless/utils logger did not seem to work in a worker thread, but console.log did. (I didn't feel like digging into it).

@aldenquimby aldenquimby deleted the fix-1541-take2 branch August 24, 2022 21:19
@aldenquimby
Copy link
Contributor Author

@dnalborczyk yes I just upgraded to v9.2.6 and error logs are working for me now, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uncaught exception from worker thread should be logged to console
2 participants