Skip to content

Commit

Permalink
Merge pull request #60 from envoy/fang/bugfixes/59/selector-event-loo…
Browse files Browse the repository at this point in the history
…p-reference-cycle-bug

Fix event loop reference cycle bug
  • Loading branch information
fangpenlin authored Aug 15, 2018
2 parents b9679a5 + 7673fef commit 62c4996
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Sources/SelectorEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ public final class SelectorEventLoop: EventLoop {
IOUtils.setBlocking(fileDescriptor: pipeReceiver, blocking: false)
// subscribe to pipe receiver read-ready event, do nothing, just allow selector
// to be interrupted

// Notice: we use a local copy of pipeReceiver to avoid referencing self
// here, thus we won't have reference cycle problem
let localPipeReceiver = pipeReceiver
setReader(pipeReceiver) {
// consume the pipe receiver, so that it won't keep triggering read event
let size = PIPE_BUF
var bytes = Data(count: Int(size))
var readSize = 1
while readSize > 0 {
readSize = bytes.withUnsafeMutableBytes { pointer in
return SystemLibrary.read(self.pipeReceiver, pointer, Int(size))
return SystemLibrary.read(localPipeReceiver, pointer, Int(size))
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/EmbassyTests/SelectorEventLoopTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,12 @@ class SelectorEventLoopTests: XCTestCase {
}
XCTAssertEqual(readData, ["hello", "baby"])
}

func testEventLoopReferenceCycle() {
// Notice: we had a reference cycle from the setReader callback to the
// selector loop object before, we ensure that when loop is not hold
// by anybody, it should be released here
weak var loop = try! SelectorEventLoop(selector: try! TestingSelector())
XCTAssertNil(loop)
}
}

0 comments on commit 62c4996

Please sign in to comment.