Skip to content

Fix memory leaks in ThreadBarriers.swift #12212

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

Merged
merged 5 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ public class _stdlib_Barrier {
}

deinit {
let ret = _stdlib_thread_barrier_destroy(_threadBarrierPtr)
if ret != 0 {
fatalError("_stdlib_thread_barrier_destroy() failed")
}
_stdlib_thread_barrier_destroy(_threadBarrierPtr)
}

public func wait() {
Expand Down
36 changes: 21 additions & 15 deletions stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,40 @@ public func _stdlib_thread_barrier_init(
InitializeConditionVariable(barrier.pointee.cond!)
#else
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
if pthread_mutex_init(barrier.pointee.mutex!, nil) != 0 {
// FIXME: leaking memory.
return -1
}
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
if pthread_cond_init(barrier.pointee.cond!, nil) != 0 {
// FIXME: leaking memory, leaking a mutex.
guard _stdlib_thread_barrier_mutex_and_cond_init(barrier) == 0 else {
barrier.pointee.mutex!.deinitialize(count: 1)
barrier.pointee.mutex!.deallocate()
barrier.pointee.cond!.deinitialize(count: 1)
barrier.pointee.cond!.deallocate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to exit the enclosing scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Rebasing isn't as straightforward as I thought, I'll try building locally this time

return -1
}
#endif
barrier.pointee.count = count
return 0
}

private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else {
return -1
}
guard pthread_cond_init(barrier.pointee.cond!, nil) == 0 else {
pthread_mutex_destroy(barrier.pointee.mutex!)
return -1
}
return 0
}

public func _stdlib_thread_barrier_destroy(
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>
) -> CInt {
) {
#if os(Windows)
// Condition Variables do not need to be explicitly destroyed
// Mutexes do not need to be explicitly destroyed
#else
if pthread_cond_destroy(barrier.pointee.cond!) != 0 {
// FIXME: leaking memory, leaking a mutex.
return -1
}
if pthread_mutex_destroy(barrier.pointee.mutex!) != 0 {
// FIXME: leaking memory.
return -1
guard pthread_cond_destroy(barrier.pointee.cond!) == 0 &&
pthread_mutex_destroy(barrier.pointee.mutex!) == 0 else {
fatalError("_stdlib_thread_barrier_destroy() failed")
}
#endif
barrier.pointee.cond!.deinitialize(count: 1)
Expand All @@ -105,7 +111,7 @@ public func _stdlib_thread_barrier_destroy(
barrier.pointee.mutex!.deinitialize(count: 1)
barrier.pointee.mutex!.deallocate()

return 0
return
}

public func _stdlib_thread_barrier_wait(
Expand Down
3 changes: 1 addition & 2 deletions validation-test/stdlib/StringSlicesConcurrentAppend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ StringTestSuite.test("SliceConcurrentAppend") {
expectEqual(0, joinRet1)
expectEqual(0, joinRet2)

ret = _stdlib_thread_barrier_destroy(barrierVar!)
expectEqual(0, ret)
_stdlib_thread_barrier_destroy(barrierVar!)

barrierVar!.deinitialize(count: 1)
barrierVar!.deallocate()
Expand Down