-
Notifications
You must be signed in to change notification settings - Fork 459
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-15133] Crash when deallocating a never-resumed DispatchSourceTimer #604
Comments
It also crashes when running on the command line with `swift run`, btw. |
@swift-ci create |
Same in Xcode 13 release version. |
On Linux w/ Swift version 5.6-dev (LLVM ca88d53176e346a, Swift a382f589a5ec230), this seems to work. It still fails on macOS 12 with Swift development toolchain 2021-09-18. |
After digging deeper, it seems that the actual crash happens when deallocating the `DispatchTimerSource`. Obviously `libdispatch` is to blame here and it has nothing to do with the continuation. I will adjust the metadata accordingly. |
I have encountered the bug in my codebase too. And I can reproduce it stably with your great example code. After some digging I can create a more simple reproductive code snippet. // main.swift
import Foundation
import Dispatch
class Command {
let timer: DispatchSourceTimer
init() {
self.timer = DispatchSource.makeTimerSource()
}
}
func test() {
var command: Command? = Command()
command = nil
}
test()
dispatchMain() Checking the swift-corelibs-libdispatch/src/queue.c Lines 2879 to 2893 in 542b7f3
The crash reason is that the object is in a inactive state. It has nothing to do with It is because we create the object but dealloc it before calling The solution is always calling activate/resume after you create the DispatchSourceTimer object. class X {
private var timer: DispatchSourceTimer!
func y() {
if timer == nil {
timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global(qos: .userInteractive))
timer.resume() // Or timer.activate()
}
}
} |
Environment
macOS 12 beta, Xcode 13 (13A233).
Additional Detail from JIRA
md5: 61e077b082169a945b058843ee5c3245
Issue Description:
Please consider the following example program which I have uploaded to
https://github.com/mickeyl/swift-bugs/tree/main/SR15133
Running it from within Xcode crashes with the following backtrace:
(lldb) bt
thread Linux port hdd cp1 #3, queue = 'com.apple.root.default-qos.cooperative', stop reason = EXC_BREAKPOINT (code=1, subcode=0x100624d9c)
frame #0: 0x0000000100624d9c libdispatch.dylib`_dispatch_queue_xref_dispose.cold.2 + 36
frame Typos #1: 0x00000001005ea2ec libdispatch.dylib`_dispatch_queue_xref_dispose + 64
frame Update autotools buildsystem for libdispatch-500.1.5 and testsuite #2: 0x00000001005e17f8 libdispatch.dylib`-[OS_dispatch_source _xref_dispose] + 28
frame Linux port hdd cp1 #3: 0x00000001000060f8 SR15133`StreamCommand.deinit(self=0x0000000105105820) at StreamCommandQueue.swift:0
frame Fix Typos #4: 0x0000000100006120 SR15133`StreamCommand.__deallocating_deinit(self=0x0000000105105820) at StreamCommandQueue.swift:0
frame libdispatch now builds on Linux and ping/pong test runs successfully. #5: 0x00000001a2f6b774 libswiftCore.dylib`_swift_release_dealloc + 56
frame Add & use DISPATCH_NOESCAPE attribute macro #6: 0x00000001000081bc SR15133`StreamCommandQueue.handleErrorCondition(stream=0x0000000100712b90, event=.errorOccurred, self=0x0000000100712e90) at StreamCommandQueue.swift:182:9
frame Add the download command of the library that libdispatch depends #7: 0x0000000100009040 SR15133`closure Linux port hdd cp1 #3 in StreamCommandQueue.stream(self=0x0000000100712e90, aStream=0x0000000100712b90, eventCode=.errorOccurred) at StreamCommandQueue.swift:202:44
frame Linux port hdd timers2 #8: 0x000000010000ac48 SR15133`partial apply for closure Linux port hdd cp1 #3 in StreamCommandQueue.stream(_:handle🙂 at <compiler-generated>:0
frame enable subdir-objects for automake #9: 0x0000000100005d5c SR15133`thunk for @escaping @callee_guaranteed @sendable @async () -> () at <compiler-generated>:0
frame Clang and linux portability fixes #10: 0x000000010000ad9c SR15133`thunk for @escaping @callee_guaranteed @sendable @async () -> ()partial apply at <compiler-generated>:0
frame Add some details on current build steps for Linux. #11: 0x000000010000bfac SR15133`thunk for @escaping @callee_guaranteed @sendable @async () -> (@out A) at <compiler-generated>:0
frame Add AC_SEARCH_LIBS stanzas for kqueue and pthread_workqueue #12: 0x000000010000cc5c SR15133`partial apply for thunk for @escaping @callee_guaranteed @sendable @async () -> (@out A) at <compiler-generated>:0
(lldb)
I suspect there is a memory management problem when the stored continuation in the `StreamCommand` gets deallocated due to `self.activeCommand = nil` while the continuation may continue to run in another task.
The text was updated successfully, but these errors were encountered: