Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions src/async/imap/MCIMAPAsyncConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ void IMAPAsyncConnection::scheduleReconnect()
mSession->scheduleReconnect();
}

void IMAPAsyncConnection::cancelStream()
{
mSession->interruptCurrentCommand();
}

unsigned int IMAPAsyncConnection::operationsCount()
{
return mQueue->count();
Expand Down
4 changes: 4 additions & 0 deletions src/async/imap/MCIMAPAsyncConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ namespace mailcore {
// Makes the next command on this connection rebuild it first; same contract as
// IMAPSession::scheduleReconnect(). Declared last, like needsReconnect().
virtual void scheduleReconnect();
// Cuts this connection's stream whatever is running on it - IMAPSession::interruptCurrentCommand()
// without the queue's check that an operation is running. For tests, which need a cut that
// lands with nothing on the wire. Declared last, like needsReconnect().
virtual void cancelStream();
};

}
Expand Down
6 changes: 6 additions & 0 deletions src/async/imap/MCIMAPOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ void IMAPOperation::itemsProgressOnMainThread(void * ctx)

void IMAPOperation::beforeMain()
{
// Runs before main() while the connection's queue thread waits for it, so no command of this
// connection is on the wire: the one place a stream cut by interruptCurrentCommand() can be
// turned into a reconnect without landing inside another operation's command sequence.
if (mSession != NULL) {
mSession->session()->scheduleReconnectIfInterrupted();
Comment thread
dbezverkhnii marked this conversation as resolved.
Resolved
}
}

bool IMAPOperation::interruptCurrentCommand()
Expand Down
9 changes: 4 additions & 5 deletions src/async/imap/MCIMAPOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ namespace mailcore {
the operations queued behind it (a disconnect, most importantly) run immediately.
Does nothing when the operation is not the one running.

Teardown of this connection only - the cut command fails with a connection error and the
connection is rebuilt before the one after; landing with no command on the wire, it is the
next command that fails and the one after that rebuilds - so call it for a command that is
being abandoned (cancelled, or given up on), never to hurry up a command whose result still
matters.
Teardown of this connection only - a command in flight fails with a connection error, a
cut that met none fails nothing, and either way the connection is rebuilt before the next
operation's first command - so call it for a command that is being abandoned (cancelled,
or given up on), never to hurry up a command whose result still matters.
Returns whether this operation was the one the queue was running at that moment. That may
include a command that finished just as the interrupt landed: its result is intact, but
the stream is cancelled all the same. */
Expand Down
1 change: 1 addition & 0 deletions src/c/imap/CIMAPAsyncConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ C_SYNTHESIZE_FUNC_WITH_SCALAR(unsigned int, operationsCount)
C_SYNTHESIZE_FUNC_WITH_SCALAR(double, lastLoginTime)
C_SYNTHESIZE_FUNC_WITH_OBJ(CIMAPBaseOperation, disconnectOperation)
C_SYNTHESIZE_FUNC_WITH_VOID(scheduleReconnect)
C_SYNTHESIZE_FUNC_WITH_VOID(cancelStream)
1 change: 1 addition & 0 deletions src/c/imap/CIMAPAsyncConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {

C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, CIMAPBaseOperation, disconnectOperation)
C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, void, scheduleReconnect)
C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, void, cancelStream)

#ifdef __cplusplus
}
Expand Down
21 changes: 17 additions & 4 deletions src/core/imap/MCIMAPSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ void IMAPSession::init()
mAutomaticConfigurationEnabled = true;
mAutomaticConfigurationDone = false;
mShouldDisconnect = false;
mStreamCancelled = false;
mLoginResponse = NULL;
mGmailUserDisplayName = NULL;
mUnparsedResponseData = NULL;
Expand Down Expand Up @@ -652,6 +653,7 @@ void IMAPSession::unsetup()
imap = mImap;
mImap = NULL;
mIdleEnabled = false;
mStreamCancelled = false;
UNLOCK();

if (imap != NULL) {
Expand Down Expand Up @@ -1120,8 +1122,9 @@ void IMAPSession::login(ErrorCode * pError)
// identity() starts with connectIfNeeded(): with mShouldDisconnect raised meanwhile it tears the
// connection down and rebuilds it, and its result is ignored above. A rebuilt connection is not
// logged in - and a rebuild that failed has no mImap at all - so this must not read as a
// successful login; nor may one whose ignored ID left the stream marked for teardown.
if (mState != STATE_LOGGEDIN || mShouldDisconnect) {
// successful login; nor may one whose ignored ID left the stream marked for teardown, or
// whose stream was cut between two of its commands.
if (mState != STATE_LOGGEDIN || mShouldDisconnect || mStreamCancelled) {
Comment thread
dbezverkhnii marked this conversation as resolved.
Resolved
* pError = ErrorConnection;
return;
}
Expand Down Expand Up @@ -3759,7 +3762,10 @@ void IMAPSession::interruptCurrentCommand()
// Deliberately not raising mShouldDisconnect here: the command this cuts fails with a
// stream error and raises it itself, at a point its caller checks. Raised from this thread
// it can land between two commands of one login(), where the nested connectIfNeeded()
// would rebuild the connection under a caller that ignores the result.
// would rebuild the connection under a caller that ignores the result. Every cut is
// remembered in mStreamCancelled: one that met no read in flight failed no command, and
// gets its reconnect at the next operation boundary (scheduleReconnectIfInterrupted).
mStreamCancelled = true;
}
UNLOCK();
}
Expand Down Expand Up @@ -4437,14 +4443,21 @@ bool IMAPSession::isDisconnected()

bool IMAPSession::needsReconnect()
{
return mState == STATE_DISCONNECTED || mShouldDisconnect;
return mState == STATE_DISCONNECTED || mShouldDisconnect || mStreamCancelled;
}

void IMAPSession::scheduleReconnect()
{
mShouldDisconnect = true;
}

void IMAPSession::scheduleReconnectIfInterrupted()
{
if (mStreamCancelled) {
mShouldDisconnect = true;
}
}

double IMAPSession::lastLoginTime()
{
LOCK();
Expand Down
7 changes: 7 additions & 0 deletions src/core/imap/MCIMAPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ namespace mailcore {
// connectIfNeeded() inside that operation - login() then fails with ErrorConnection.
// Declared last, for the same reason as needsReconnect().
virtual void scheduleReconnect();
// Turns a stream cut by interruptCurrentCommand() into a pending reconnect. For the moment
// between two operations, when no command is on the wire: a cut that landed with no read
// in flight failed nothing, and this is where it gets its reconnect. Declared last.
virtual void scheduleReconnectIfInterrupted();

private:
String * mHostname;
Expand Down Expand Up @@ -326,6 +330,9 @@ namespace mailcore {
bool mAutomaticConfigurationDone;
// Read cross-thread with mState, and for the same reason: see above.
std::atomic<bool> mShouldDisconnect;
// The stream was cancelled by interruptCurrentCommand() and nothing has rebuilt it yet.
// Written by the interrupting thread, read cross-thread like mShouldDisconnect.
std::atomic<bool> mStreamCancelled;

String * mLoginResponse;
String * mGmailUserDisplayName;
Expand Down
1 change: 1 addition & 0 deletions src/include/MailCore/CIMAPAsyncConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {

C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, CIMAPBaseOperation, disconnectOperation)
C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, void, scheduleReconnect)
C_SYNTHESIZE_FUNC_DEFINITION(CIMAPAsyncConnection, void, cancelStream)

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions src/include/MailCore/MCIMAPAsyncConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ namespace mailcore {
// Makes the next command on this connection rebuild it first; same contract as
// IMAPSession::scheduleReconnect(). Declared last, like needsReconnect().
virtual void scheduleReconnect();
// Cuts this connection's stream whatever is running on it - IMAPSession::interruptCurrentCommand()
// without the queue's check that an operation is running. For tests, which need a cut that
// lands with nothing on the wire. Declared last, like needsReconnect().
virtual void cancelStream();
};

}
Expand Down
9 changes: 4 additions & 5 deletions src/include/MailCore/MCIMAPOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ namespace mailcore {
the operations queued behind it (a disconnect, most importantly) run immediately.
Does nothing when the operation is not the one running.

Teardown of this connection only - the cut command fails with a connection error and the
connection is rebuilt before the one after; landing with no command on the wire, it is the
next command that fails and the one after that rebuilds - so call it for a command that is
being abandoned (cancelled, or given up on), never to hurry up a command whose result still
matters.
Teardown of this connection only - a command in flight fails with a connection error, a
cut that met none fails nothing, and either way the connection is rebuilt before the next
operation's first command - so call it for a command that is being abandoned (cancelled,
or given up on), never to hurry up a command whose result still matters.
Returns whether this operation was the one the queue was running at that moment. That may
include a command that finished just as the interrupt landed: its result is intact, but
the stream is cancelled all the same. */
Expand Down
7 changes: 7 additions & 0 deletions src/include/MailCore/MCIMAPSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ namespace mailcore {
// connectIfNeeded() inside that operation - login() then fails with ErrorConnection.
// Declared last, for the same reason as needsReconnect().
virtual void scheduleReconnect();
// Turns a stream cut by interruptCurrentCommand() into a pending reconnect. For the moment
// between two operations, when no command is on the wire: a cut that landed with no read
// in flight failed nothing, and this is where it gets its reconnect. Declared last.
virtual void scheduleReconnectIfInterrupted();

private:
String * mHostname;
Expand Down Expand Up @@ -326,6 +330,9 @@ namespace mailcore {
bool mAutomaticConfigurationDone;
// Read cross-thread with mState, and for the same reason: see above.
std::atomic<bool> mShouldDisconnect;
// The stream was cancelled by interruptCurrentCommand() and nothing has rebuilt it yet.
// Written by the interrupting thread, read cross-thread like mShouldDisconnect.
std::atomic<bool> mStreamCancelled;

String * mLoginResponse;
String * mGmailUserDisplayName;
Expand Down
7 changes: 7 additions & 0 deletions src/swift/imap/IMAPAsyncConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public class MCOIMAPAsyncConnection: NSObjectCompat {
connection.scheduleReconnect()
}

/// Cuts this connection's stream whatever is running on it. For tests: the interrupt that lands
/// with nothing on the wire, which MCOIMAPBaseOperation.interruptCurrentCommand() cannot
/// produce - it only cuts a command that is in flight.
internal func cancelStream() {
connection.cancelStream()
}

internal var isReserved: Bool {
return connection.isReserved
}
Expand Down
8 changes: 4 additions & 4 deletions src/swift/imap/IMAPBaseOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class MCOIMAPBaseOperation : MCOOperation {
the one running.

Unlike cancel(), which only raises a flag mailcore checks before starting an operation, this
reaches the command already in flight. It costs the connection: the cut command fails with a
connection error and the connection is rebuilt before the one after (landing with no command on
the wire, it is the next command that fails and the one after that rebuilds), so call it for a
command being abandoned, never to hurry up one whose result still matters.
reaches the command already in flight. It costs the connection: a command in flight fails with
a connection error, a cut that met none fails nothing, and either way the connection is rebuilt
before the next operation's first command - so call it for a command being abandoned, never to
hurry up one whose result still matters.

- Returns: whether this operation was the one the queue was running at that moment. That may
include a command that finished just as the interrupt landed: its result is intact, but the
Expand Down
47 changes: 47 additions & 0 deletions unittest/IMAPConnectionLeaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,53 @@ final class IMAPConnectionLeaseTests: XCTestCase {
}
}

func testCutWithNothingOnTheWireReconnectsBeforeTheNextOperation() throws {
// Every command is answered, so the NOOPs complete and the cut below meets no read in
// flight - the case an interrupt of a running command cannot produce.
let endpoint = try LeaseTestTCPEndpoint(greeting: "* OK [CAPABILITY IMAP4rev1] LeaseTestTCPEndpoint ready\r\n",
answers: ["LOGIN": "",
"CAPABILITY": "* CAPABILITY IMAP4rev1\r\n",
"LIST": "* LIST (\\Noselect) \"/\" \"\"\r\n",
"NOOP": ""])
defer { endpoint.stop() }

let session = makeSession(port: endpoint.port, maximumConnections: 1)

guard let leased = session.acquireConnection(folder: nil) else {
return XCTFail("An empty pool with room for 1 connection must satisfy the lease")
}

runOffMainThread(timeout: 30) {
// A NOOP on a never-connected connection is a no-op, so connect first.
let connect = session.connectOperation()
connect.setConnection(leased)
XCTAssertEqual(self.start(connect).wait(timeout: .now() + 5), .success)
let first = session.noopOperation()
first.setConnection(leased)
XCTAssertEqual(self.start(first).wait(timeout: .now() + 5), .success)
XCTAssertEqual(endpoint.acceptedClientCount, 1)

// Logged in and idle: nothing fails on this cut, so nothing raises the reconnect flag.
leased.cancelStream()

// The next operation must rebuild the connection first instead of paying its own
// first command to find out.
let second = session.noopOperation()
second.setConnection(leased)
var error: Error?
let finished = DispatchSemaphore(value: 0)
second.start { opError in
error = opError
finished.signal()
}
XCTAssertEqual(finished.wait(timeout: .now() + 5), .success)
XCTAssertNil(error, "The NOOP after a cut with nothing on the wire must not fail")
XCTAssertEqual(endpoint.acceptedClientCount, 2, "The cut connection was expected to reconnect before the NOOP")

session.releaseConnection(leased, disconnect: false)
}
}

func testUnpinnedOperationAvoidsTheLeasedConnection() throws {
let endpoint = try LeaseTestTCPEndpoint()
defer { endpoint.stop() }
Expand Down
Loading