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

**URL:** <https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122>\
**Category:** Local Setup\
**Created:** [August 6, 2019, 12:30pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122 "2019-08-06T12:30:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohsenyz](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mohsenyz](https://forum.heroiclabs.com/u/mohsenyz)\
**Post date:** [August 6, 2019, 12:30pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/1 "2019-08-06T12:30:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)\
**Post date:** [August 6, 2019, 1:15pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/2 "2019-08-06T13:15:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohsenyz](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@mohsenyz](https://forum.heroiclabs.com/u/mohsenyz)\
**Post date:** [August 7, 2019, 2:28pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/3 "2019-08-07T14:28:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zyro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/zyro/32/14_2.png) [@zyro](https://forum.heroiclabs.com/u/zyro)\
**Post date:** [August 7, 2019, 9:29pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/4 "2019-08-07T21:29:52Z")

</div>

You can pass the work (or some part of it) to a background [goroutine](https://tour.golang.org/concurrency/1) 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](https://heroiclabs.com/docs/runtime-code-function-reference/#http).

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.

---

<div class="post-metadata">

**Author:** ![eduardomoroni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/eduardomoroni/32/2474_2.png) [@eduardomoroni](https://forum.heroiclabs.com/u/eduardomoroni)\
**Post date:** [May 15, 2025, 10:24pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/5 "2025-05-15T22:24:41Z")

</div>

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

```auto
  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

```auto
[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.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [May 16, 2025, 9:11am UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/6 "2025-05-16T09:11:37Z")

</div>

@eduardomoroni what’s the value of `timeout`?

---

<div class="post-metadata">

**Author:** ![eduardomoroni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/eduardomoroni/32/2474_2.png) [@eduardomoroni](https://forum.heroiclabs.com/u/eduardomoroni)\
**Post date:** [May 16, 2025, 12:42pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/7 "2025-05-16T12:42:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [May 16, 2025, 12:56pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/8 "2025-05-16T12:56:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eduardomoroni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/eduardomoroni/32/2474_2.png) [@eduardomoroni](https://forum.heroiclabs.com/u/eduardomoroni)\
**Post date:** [May 16, 2025, 1:07pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/9 "2025-05-16T13:07:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [May 16, 2025, 2:27pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/10 "2025-05-16T14:27:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eduardomoroni](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/eduardomoroni/32/2474_2.png) [@eduardomoroni](https://forum.heroiclabs.com/u/eduardomoroni)\
**Post date:** [May 16, 2025, 2:46pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/11 "2025-05-16T14:46:19Z")

</div>

Looking at the logs

```auto
[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.

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [May 19, 2025, 6:21pm UTC](https://forum.heroiclabs.com/t/error-context-canceled-and-context-deadline-exceed/122/12 "2025-05-19T18:21:00Z")

</div>

@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.
