Error : "Context canceled" and "Context deadline exceed"

Hello everybody, I’ve many errors about “Context canceled” and “Context deadline exceed” in important parts of RPC codes, Like executing SQL, updating wallets and … Which causes scripts to be run incompletely, and making the bad experience for end-users.
How can I prevent these errors in server/app side? What’s the difference between “Context canceled” and “Context deadline exceed”? and How can I force my script to be run even when context is canceled?

1 Like

Context cancelled is caused when the original request was cancelled. Most frequently, it refers to the client connection going away and therefore Nakama cancels in-flight requests (to various subsystems, including the database) to avoid performing unnecessary work.

“Context deadline exceeded” means that the request took longer to finish than allowed and the system killed the request. This could refer to the client connections, or database connections or any other subsystem.

Most of the time, this could happen when interacting with a query that is performing poorly on the database. In this case, I’d suggest checking your query and your database settings to ensure optimum performance.

2 Likes

Thanks,
In my scenario, I have an RPC that does many works (writing score in many leaderboards, sending notifications, giving the user some rewards (Updating wallet))
What’s the best practice to handle this scenario with the nakama, within avoiding errors like “Context deadline exceed”?
Should I pass works like “Sending notifications” to a message broker server (Ex: RabbitMQ) to be run with delay?

You can pass the work (or some part of it) to a background goroutine if you’re using the Go runtime. With Lua your best bet is to hand off the work to another service via a HTTP request.

It’s not very common to need to do this but it seems you’re trying to do a lot of work in one RPC function and the client is timing out waiting for a response.

1 Like

Sorry for reviving an old thread, but I’m facing context deadline exceeded and the operation is not taking that long

Here’s the code

  try {
    const url = `${ctx.env.API_BASE_URL}/api/webhooks/nakama/${rpc.procedure}/`;
    logger.info("Requesting %s with payload: %s", url, body);
    logger.info("Headers: %s", JSON.stringify(headers));
    res = nk.httpRequest(url, method, headers, body, timeout);
    logger.debug("Response: %s", JSON.stringify(res));
  } catch (error) {
    logger.error("Error in httpRequestRPC: %s", error.message);
    throw error;
  }

and the log lines

[1] lorcanito-nakama  | {"level":"info","ts":"2025-05-15T22:17:31.918Z","caller":"server/runtime_javascript_logger.go:74","msg":"Requesting http://host.docker.internal:3000/api/webhooks/nakama/drop-player/ with payload: {\"gameId\":\"bd4a1900-b158-4255-9c18-250c10ed828c\",\"matchId\":\"bd4a1900-b158-4255-9c18-250c10ed828c\",\"winnerId\":\"user_2lWkF6CezRvtEF4WVYbeV5jbZDS\",\"loserId\":\"user_2lTrwWGreQ6nMxH5hNrVPfxPCvq\",\"reason\":\"timeout\"}","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"info","ts":"2025-05-15T22:17:31.918Z","caller":"server/runtime_javascript_logger.go:74","msg":"Headers: {\"X-Api-Secret\":\"\",\"Content-Type\":\"application/json\",\"Accept\":\"application/json\"}","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"error","ts":"2025-05-15T22:17:31.952Z","caller":"server/runtime_javascript_logger.go:94","msg":"Error in httpRequestRPC: HTTP request error: Post \"http://host.docker.internal:3000/api/webhooks/nakama/drop-player\": context deadline exceeded","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"error","ts":"2025-05-15T22:17:31.952Z","caller":"server/runtime_javascript_logger.go:94","msg":"[Message Loop] An error has occurred: HTTP request error: Post \"http://host.docker.internal:3000/api/webhooks/nakama/drop-player\": context deadline exceeded","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}

Sometimes the requests go through, but I have spikes in this error frequently, like 8 times a day for around 15min.
It might be a long shot, but I’d appreciate any insights.

@eduardomoroni what’s the value of timeout?

I’m playing around with it. It started at 30, then 15, now I completely removed.
I noticed that with the default timeout (aka, not passing anything to the function) I see fewer problems. But they still happen, looking at my service response, it averages 1.5s response time.

@eduardomoroni the value is set in ms default is 5000, so if that’s not enough you should try 10000.

Looking at the logs, I get the “context deadline exceeded” in 50ms-ish.

Is this happening due to how we stream logs? Is there a way of debugging this problem on my end? Locally, i have a really low latency and I’m still getting the error rather frequently

no, this shouldn’t be a logger streaming issue. You’re saying you’re still seeing “context deadline exceeded” with a 10000ms timeout?

Looking at the logs

[1] lorcanito-nakama  | {"level":"info","ts":"2025-05-15T22:17:31.918Z","caller":"server/runtime_javascript_logger.go:74","msg":"Requesting http://host.docker.internal:3000/api/webhooks/nakama/drop-player/ with payload: {\"gameId\":\"bd4a1900-b158-4255-9c18-250c10ed828c\",\"matchId\":\"bd4a1900-b158-4255-9c18-250c10ed828c\",\"winnerId\":\"user_2lWkF6CezRvtEF4WVYbeV5jbZDS\",\"loserId\":\"user_2lTrwWGreQ6nMxH5hNrVPfxPCvq\",\"reason\":\"timeout\"}","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"info","ts":"2025-05-15T22:17:31.918Z","caller":"server/runtime_javascript_logger.go:74","msg":"Headers: {\"X-Api-Secret\":\"\",\"Content-Type\":\"application/json\",\"Accept\":\"application/json\"}","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"error","ts":"2025-05-15T22:17:31.952Z","caller":"server/runtime_javascript_logger.go:94","msg":"Error in httpRequestRPC: HTTP request error: Post \"http://host.docker.internal:3000/api/webhooks/nakama/drop-player\": context deadline exceeded","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}
[1] lorcanito-nakama  | {"level":"error","ts":"2025-05-15T22:17:31.952Z","caller":"server/runtime_javascript_logger.go:94","msg":"[Message Loop] An error has occurred: HTTP request error: Post \"http://host.docker.internal:3000/api/webhooks/nakama/drop-player\": context deadline exceeded","mid":"bd4a1900-b158-4255-9c18-250c10ed828c"}

The request starts at 2025-05-15T22:17:31.918Z and we receive the error message at 2025-05-15T22:17:31.952Z. And I also see this behaviour in production.

  • Request fails before even being sent
  • Requests go through, but fail before being resolved (harder to replicate)

Weirdly enough, the higher I set the timeout on nakama side the more errors I have.

@eduardomoroni I think it’s best if you open a new topic stating your issue as we’re running off-topic here. Please provide some more context on where is the code snippet being called and in what circumstances, it may help us debug.

1 Like