What exactly is a cancellation point?

When your thread gets pulled from execution, its state is saved by the OS and that is not a cancellation of the thread. The cancellation means thread termination, on request, with the specific intent of letting everything in a final state when completed (aka. all resources are freed, all handlers are updated, etc.). What you … Read more

How to cancel all remaining tasks in gather if one fails?

The problem with your implementation is that it calls sleepers.cancel() after sleepers has already raised. Technically the future returned by gather() is in a completed state, so its cancellation must be no-op. To correct the code, you just need to cancel the children yourself instead of trusting gather‘s future to do it. Of course, coroutines … Read more

How to cancel timeout inside of Javascript Promise?

Edit 2021 all platforms have converged on AbortController as the cancellation primitive and there is some built in support for this. In Node.js // import { setTimeout } from ‘timers/promises’ // in ESM const { setTimeout } = require(‘timers/promises’); const ac = new AbortController(); // cancellable timeout (async () => { await setTimeout(1000, null, { … Read more

How to stop a DispatchWorkItem in GCD?

GCD does not perform preemptive cancelations. So, to stop a work item that has already started, you have to test for cancelations yourself. In Swift, cancel the DispatchWorkItem. In Objective-C, call dispatch_block_cancel on the block you created with dispatch_block_create. You can then test to see if was canceled or not with isCancelled in Swift (known … Read more

How to “sleep” until timeout or cancellation is requested

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

How to use the CancellationToken without throwing/catching an exception?

You can implement your work method as follows: private static void Work(CancellationToken cancelToken) { while (true) { if(cancelToken.IsCancellationRequested) { return; } Console.Write(“345”); } } That’s it. You always need to handle cancellation by yourself – exit from method when it is appropriate time to exit (so that your work and data is in consistent state) … Read more

What is the Correct HTTP Status Code for a Cancelled Request

To be consistent I would suggest 400 Bad Request now if your backend apps are capable of identifying when the client gets disconnected or if you reject or close the connection, probably you could return Nginx’ non-standard code 499 or 444. 499 Client Closed Request Used when the client has closed the request before the … Read more

How to “sleep” until timeout or cancellation is requested in .NET 4.0

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

Is there a way to short circuit async/await flow?

The standard way to do this now is through AbortSignals async function update({ signal } = {}) { // pass these to methods to cancel them internally in turn // this is implemented throughout Node.js and most of the web platform try { var urls = await getCdnUrls({ signal }); var metadata = await fetchMetaData(urls); … Read more

tech