Skip to content

Commit 2422c65

Browse files
durable objects: FURTHER clarify blockConcurrencyWhile (#21326)
* durable objects: FURTHER clarify blockConcurrencyWhile * Update src/content/docs/durable-objects/api/state.mdx Co-authored-by: Lambros Petrou <[email protected]> --------- Co-authored-by: Lambros Petrou <[email protected]>
1 parent 1a15ffe commit 2422c65

File tree

1 file changed

+12
-3
lines changed
  • src/content/docs/durable-objects/api

1 file changed

+12
-3
lines changed

Diff for: src/content/docs/durable-objects/api/state.mdx

+12-3
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,21 @@ The request context for a Durable Objects extends at least 60 seconds after the
7373

7474
`blockConcurrencyWhile` executes an async callback while blocking any other events from being delivered to the Durable Object until the callback completes. This method guarantees ordering and prevents concurrent requests. All events that were not explicitly initiated as part of the callback itself will be blocked. Once the callback completes, all other events will be delivered.
7575

76-
`blockConcurrencyWhile` is commonly used within the constructor of the Durable Object class to enforce initialization to occur before any requests are delivered. Another use case is executing `async` operations based on the current state of the Durable Object and using `blockConcurrencyWhile` to prevent that state from changing while yielding the event loop.
77-
78-
If the callback throws an exception, the object will be terminated and reset. This ensures that the object cannot be left stuck in an uninitialized state if something fails unexpectedly. To avoid this behavior, enclose the body of your callback in a `try...catch` block to ensure it cannot throw an exception.
76+
* `blockConcurrencyWhile` is commonly used within the constructor of the Durable Object class to enforce initialization to occur before any requests are delivered.
77+
* Another use case is executing `async` operations based on the current state of the Durable Object and using `blockConcurrencyWhile` to prevent that state from changing while yielding the event loop.
78+
* If the callback throws an exception, the object will be terminated and reset. This ensures that the object cannot be left stuck in an uninitialized state if something fails unexpectedly.
79+
* To avoid this behavior, enclose the body of your callback in a `try...catch` block to ensure it cannot throw an exception.
7980

8081
To help mitigate deadlocks there is a 30 second timeout applied when executing the callback. If this timeout is exceeded, the Durable Object will be reset. It is best practice to have the callback do as little work as possible to improve overall request throughput to the Durable Object.
8182

83+
:::note
84+
85+
You should only need `blockConcurrencyWhile` if you are making additional, asynchronous calls (such as to another API or service), and cannot tolerate other requests processed by the Durable Object changing its internal while the event loop is yielded from the original request.
86+
87+
In practice, this is quite rare, and most use cases do not need `blockConcurrencyWhile`.
88+
89+
:::
90+
8291
```js
8392
// Durable Object
8493
export class MyDurableObject extends DurableObject {

0 commit comments

Comments
 (0)