Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3028] do-catch block break nested DispatchSource workitem #714

Closed
swift-ci opened this issue Oct 23, 2016 · 2 comments
Closed

[SR-3028] do-catch block break nested DispatchSource workitem #714

swift-ci opened this issue Oct 23, 2016 · 2 comments

Comments

@swift-ci
Copy link

Previous ID SR-3028
Radar None
Original Reporter michael-yuji (JIRA User)
Type Bug
Status Closed
Resolution Invalid
Environment

OS X 10.12

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug
Assignee None
Priority Medium

md5: 13ab7af38c18a6a0a6f154f8ac1b81f0

Issue Description:

Basically, when use DispatchSource(tested Timer and FileSystem) within a do-catch block will break the GCD source.

Consider the code below, it works fine without do-catch block but stop working when the block is present. Since explicitly referencing source inside the eventhandler will solve the program despite nested in do-catch block.

My educational guess is that somehow the do-catch block is breaking the dispatch_retain / dispatch_release cycle.

// main.swift
import Dispatch
do { // remove the try-catch block the timer works fine
    let source = DispatchSource.makeTimerSource()
    source.setEventHandler {
        /* uncomment the next line, explicitly reference `source` will resolve the issue as well */
//     _ = source
        print("hello world")
    }
    source.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTimerInterval.seconds(1))
    source.resume()
} catch {}
dispatchMain()
@belkadan
Copy link

Dispatch folks, does a DispatchSource need to be retained while it's active? If so, this pretty much behaves as expected, Yuuji; objects may be released as soon as nothing refers to them anymore. The fact that it works without a do-catch block is mostly a coincidence.

@swift-ci
Copy link
Author

Comment by Pierre Habouzit (JIRA)

This is a behaves correctly, releasing the last reference on a source is an implicit cancelation. The best way to have it not happen is to indeed have a reference to the source from one of the handler.

calling `cancel` on the source will cause the handler to be destroyed and the retain cycle to be broken.

the C interface works exactly the same way.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from swiftlang/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants