<!--
{
  "availability" : [
    "iOS: 4.0.0 -",
    "iPadOS: 4.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.6.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Dispatch",
  "identifier" : "/documentation/Dispatch/dispatch_source_cancel",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Dispatch"
    ],
    "preciseIdentifier" : "c:@F@dispatch_source_cancel"
  },
  "title" : "dispatch_source_cancel"
}
-->

# dispatch_source_cancel

Asynchronously cancels the dispatch source, preventing any further invocation of its event handler block.

```
extern void dispatch_source_cancel(dispatch_source_t source);
```

## Parameters

`source`

The dispatch source to be canceled. This parameter cannot be `NULL`.

## Discussion

Cancellation prevents any further invocation of the event handler block for the specified dispatch source, but does not interrupt an event handler block that is already in progress. The optional cancellation handler is submitted to the target queue once the event handler block has been completed.

The cancellation handler is submitted to the source’s target queue when the source’s event handler has finished, indicating that it is safe to close the source’s handle (file descriptor or mach port).

The optional cancellation handler is submitted to the dispatch source object’s target queue only after the system has released all of its references to any underlying system objects (file descriptors or mach ports). Thus, the cancellation handler is a convenient place to close or deallocate such system objects. Note that it is invalid to close a file descriptor or deallocate a mach port currently being tracked by a dispatch source object before the cancellation handler is invoked.

## See Also

[`dispatch_source_set_cancel_handler`](/documentation/Dispatch/dispatch_source_set_cancel_handler)

Sets the cancellation handler block for the given dispatch source.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)