File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
durabletask-js-azuremanaged/src
durabletask-js/src/worker Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -125,8 +125,8 @@ export class DurableTaskAzureManagedClientBuilder {
125125 }
126126
127127 /**
128- * Sets the logger to use for logging.
129- * Defaults to ConsoleLogger.
128+ * Sets the logger to use for logging.
129+ * Defaults to ConsoleLogger.
130130 *
131131 * @param logger The logger instance.
132132 * @returns This builder instance.
Original file line number Diff line number Diff line change @@ -282,8 +282,27 @@ export class TaskHubGrpcWorker {
282282 // Cancel stream first while error handlers are still attached
283283 // This allows the error handler to suppress CANCELLED errors
284284 this . _responseStream ?. cancel ( ) ;
285- // Brief pause to let cancellation error propagate to handlers
286- await sleep ( 10 ) ;
285+
286+ // Wait for the stream to react to cancellation using events rather than a fixed delay.
287+ // This avoids race conditions caused by relying on timing alone.
288+ if ( this . _responseStream ) {
289+ try {
290+ await withTimeout (
291+ new Promise < void > ( ( resolve ) => {
292+ const stream = this . _responseStream ! ;
293+ // Any of these events indicates the stream has processed cancellation / is closing.
294+ stream . once ( "end" , resolve ) ;
295+ stream . once ( "close" , resolve ) ;
296+ stream . once ( "error" , ( ) => resolve ( ) ) ;
297+ } ) ,
298+ 1000 ,
299+ "Timed out waiting for response stream to close after cancellation" ,
300+ ) ;
301+ } catch {
302+ // If we time out waiting for the stream to close, proceed with forced cleanup below.
303+ }
304+ }
305+
287306 // Now safe to remove listeners and destroy
288307 this . _responseStream ?. removeAllListeners ( ) ;
289308 this . _responseStream ?. destroy ( ) ;
You can’t perform that action at this time.
0 commit comments