What to do about "timeout: context canceled" in critical after-hook

We’ve found an error in production where critical code that sets up a user’s default metadata/wallet in the After hook for AuthenticateEmail/Apple/Custom produces the error “timeout: context canceled”.

I’ve read other posts (Error : "Context canceled" and "Context deadline exceed" and Error encountered context canceled while in goroutine) explaining that if a client leaves before the request is complete, it will cancel database operations to reduce load. This makes sense if the work you’re doing has no side effects, but it’s dangerous otherwise. This behavior was a big surprise to our team.

Is there a way to disable this functionality or easily work around it in Lua?

  1. Versions: Nakama 3.10 (forked from 1d0527e0), Docker, nakama-godot (77ecd966)
  2. Server Framework Runtime language: Lua

The context cancellation is working as expected. In this case, an option is converting (only) this Hook to Go code, and spin off a Goroutine with a background context to complete the work irrespectively of the original http request.

Please note that this can open you up to a DDOS attack if many many conn attempt to open and close connections very quickly.

The alternative is to make your storage writes:

a. Write all in one batch so that either everything is written or everything fails
b. Use conditional ‘*’ version writes such that the write does not update the data that’s there in case of existing users coming back in.