forked from MailCore/mailcore2
-
Notifications
You must be signed in to change notification settings - Fork 5
COR-211, COR-212, COR-213, COR-216 - Fix IMAP login and connection lifetime crashes #113
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02358ee
Fix: a connection-scoped disconnect retains the pool that owns it COR…
dbezverkhnii 5113816
Fix: let the cut command raise the reconnect flag itself COR-211
dbezverkhnii f198be6
Fix: fail a login that identity() left not logged in COR-211
dbezverkhnii 97288a7
Fix: return a dropped lease on the session's queue COR-216
dbezverkhnii eecee81
Test: import Darwin explicitly for usleep COR-216
dbezverkhnii 2d1de87
Dev: qualify the interrupt docs for a cut with no command in flight C…
dbezverkhnii a2eb1dc
Dev: import Dispatch where deinit now dispatches COR-216
dbezverkhnii 5ea366a
Fix: fail a login whose ignored ID left the stream marked for teardow…
dbezverkhnii 35710cc
Test: keep IMAPLoginTests within the package's Swift baseline COR-211
dbezverkhnii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // | ||
| // IMAPConnectionOwnerLifetimeTests.swift | ||
| // mailcore2 | ||
| // | ||
| // A pooled connection must not outlive its IMAPAsyncSession while it still has work: the | ||
| // connection reaches its owner through a raw pointer on every queue start and stop. | ||
| // | ||
|
|
||
| #if canImport(Darwin) | ||
|
|
||
| import Darwin | ||
| import Dispatch | ||
| import Foundation | ||
| import XCTest | ||
|
dbezverkhnii marked this conversation as resolved.
|
||
|
|
||
| #if SWIFT_PACKAGE | ||
| import CMailCore | ||
| #endif | ||
|
|
||
| @testable import MailCore | ||
|
|
||
| final class IMAPConnectionOwnerLifetimeTests: XCTestCase { | ||
|
|
||
| private func makeSession() -> MCOIMAPSession { | ||
| let session = MCOIMAPSession() | ||
| session.hostname = "127.0.0.1" | ||
| session.port = 1 // never connected: a disconnect on a fresh connection is a no-op | ||
| session.connectionType = ConnectionTypeClear | ||
| session.username = "user" | ||
| session.password = "password" | ||
| session.maximumConnections = 1 | ||
| return session | ||
| } | ||
|
|
||
| private func runOffMainThread(timeout: TimeInterval, _ body: @escaping () -> Void) { | ||
| let finished = expectation(description: "test body") | ||
| DispatchQueue.global(qos: .userInitiated).async { | ||
| body() | ||
| finished.fulfill() | ||
| } | ||
| waitForExpectations(timeout: timeout) | ||
| } | ||
|
|
||
| private func start(_ operation: MCOIMAPOperation) -> DispatchSemaphore { | ||
| let finished = DispatchSemaphore(value: 0) | ||
| operation.start { _ in | ||
| finished.signal() | ||
| } | ||
| return finished | ||
| } | ||
|
|
||
| /// A connection-scoped disconnect is the one operation that does not retain the session it | ||
| /// belongs to. Queue one, let every other reference to the session go, wait for the | ||
| /// connection's queue thread to stop - its stop releases the session's last retain - and then | ||
| /// start the queued operation: the queue restarts and reports to an owner that no longer exists. | ||
| func testQueuedDisconnectKeepsTheOwnerAlive() { | ||
| var session: MCOIMAPSession? = makeSession() | ||
| var handle = session!.acquireConnection(folder: nil) | ||
| XCTAssertNotNil(handle) | ||
|
|
||
| // Retains the connection (through the operation), not the session. | ||
| let later = handle!.disconnectOperation() | ||
|
|
||
| runOffMainThread(timeout: 30) { | ||
| // Wakes the connection's queue thread; when it stops it releases its retain of the | ||
| // session, which is the last one once the Swift wrappers below are gone. | ||
| XCTAssertEqual(self.start(handle!.disconnectOperation()).wait(timeout: .now() + 5), .success) | ||
|
|
||
| session!.releaseConnection(handle!, disconnect: false) | ||
|
|
||
| // The queue thread lingers for about a second after its last operation and releases | ||
| // its retain of the session when it stops; only then are the wrappers below the last | ||
| // holders. | ||
| let deadline = Date(timeIntervalSinceNow: 10) | ||
| while session!.isOperationQueueRunning && Date() < deadline { | ||
| usleep(20_000) | ||
| } | ||
| XCTAssertFalse(session!.isOperationQueueRunning, "the connection's queue was expected to stop") | ||
| handle = nil | ||
| // The dropped handle returns its lease on the session's queue (the main queue here) | ||
| // and holds the session until then; let that run before the session is dropped. | ||
| DispatchQueue.main.sync {} | ||
| session = nil | ||
|
|
||
| XCTAssertEqual(self.start(later).wait(timeout: .now() + 5), .success, | ||
| "The queued disconnect must still run once the pool has been dropped") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.