From d8896ae0a22b0014d0b43706280c8a390f14b058 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sun, 14 Oct 2012 19:22:45 +0000 Subject: - Clean up some connection cancellation/close-during-connect edges as a result of r3894, attempting to improve some exceptions during aborted connections - Name threads created in SPMySQL.framework --- .../Source/SPMySQLConnection Categories/Delegate & Proxy.m | 11 ++++++++--- .../Source/SPMySQLConnection Categories/Ping & KeepAlive.m | 1 + Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m | 4 ++-- .../SPMySQLFramework/Source/SPMySQLFastStreamingResult.m | 2 ++ Source/SPConnectionController.m | 5 +---- Source/SPConnectionHandler.m | 6 ++++-- Source/SPDatabaseDocument.m | 6 +++++- 7 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Delegate & Proxy.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Delegate & Proxy.m index e0b745e0..9f6d8dff 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Delegate & Proxy.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Delegate & Proxy.m @@ -95,6 +95,7 @@ */ - (void)_proxyStateChange:(NSObject *)aProxy { + NSThread *reconnectionThread; // Perform no actions if this isn't the current connection proxy, or if notifications // are currently set to be ignored @@ -112,11 +113,15 @@ // Trigger a reconnect depending on connection usage recently. If the connection has // actively been used in the last couple of minutes, trigger a full reconnection attempt. if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 60 * 2) { - [NSThread detachNewThreadSelector:@selector(reconnect) toTarget:self withObject:nil]; - + reconnectionThread = [[[NSThread alloc] initWithTarget:self selector:@selector(_reconnectAllowingRetries:) object:[NSNumber numberWithBool:YES]] autorelease]; + [reconnectionThread setName:@"SPMySQL reconnection thread (full)"]; + [reconnectionThread start]; + // If used within the last fifteen minutes, trigger a background/single reconnection attempt } else if (_elapsedSecondsSinceAbsoluteTime(lastConnectionUsedTime) < 60 * 15) { - [NSThread detachNewThreadSelector:@selector(_reconnectAfterBackgroundConnectionLoss) toTarget:self withObject:nil]; + reconnectionThread = [[[NSThread alloc] initWithTarget:self selector:@selector(_reconnectAfterBackgroundConnectionLoss:) object:nil] autorelease]; + [reconnectionThread setName:@"SPMySQL reconnection thread (limited)"]; + [reconnectionThread start]; // Otherwise set the state to connection lost for automatic reconnect on next use } else { diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m index fb8a984f..43050a54 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Ping & KeepAlive.m @@ -83,6 +83,7 @@ - (void)_threadedKeepAlive { keepAliveThread = [NSThread currentThread]; + [keepAliveThread setName:@"SPMySQL connection keepalive thread"]; // If the maximum number of ping failures has been reached, determine whether to reconnect. if (keepAliveLastPingBlocked || keepAlivePingFailures >= 3) { diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m index e12063a2..bdef884c 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m @@ -211,7 +211,7 @@ const char *SPMySQLSSLPermissibleCiphers = "DHE-RSA-AES256-SHA:AES256-SHA:DHE-RS [self _cancelKeepAlives]; // Disconnect if appropriate (which should also disconnect any proxy) - [self disconnect]; + [self _disconnect]; // Clean up the connection proxy, if any if (proxy) { @@ -638,7 +638,7 @@ const char *SPMySQLSSLPermissibleCiphers = "DHE-RSA-AES256-SHA:AES256-SHA:DHE-RS if (proxy) proxyStateChangeNotificationsIgnored = YES; // Close the connection if it's active - [self disconnect]; + [self _disconnect]; // Lock the connection while waiting for network and proxy [self _lockConnection]; diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLFastStreamingResult.m b/Frameworks/SPMySQLFramework/Source/SPMySQLFastStreamingResult.m index 1fa4b829..064494f3 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLFastStreamingResult.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLFastStreamingResult.m @@ -336,6 +336,8 @@ typedef struct st_spmysqlstreamingrowdata { NSUInteger i, dataCopiedLength, rowDataLength; SPMySQLStreamingRowData *newRowStore; + [[NSThread currentThread] setName:@"SPMySQLFastStreamingResult data download thread"]; + size_t sizeOfStreamingRowData = sizeof(SPMySQLStreamingRowData); size_t sizeOfDataLengths = (size_t)(sizeof(unsigned long) * numberOfFields); size_t sizeOfChar = sizeof(char); diff --git a/Source/SPConnectionController.m b/Source/SPConnectionController.m index 23fe0911..2e619285 100644 --- a/Source/SPConnectionController.m +++ b/Source/SPConnectionController.m @@ -302,10 +302,6 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, [progressIndicatorText setStringValue:NSLocalizedString(@"Cancelling...", @"cancelling task status message")]; [progressIndicatorText display]; - - if (mySQLConnection) { - [NSThread detachNewThreadSelector:@selector(disconnect) toTarget:mySQLConnection withObject:nil]; - } #endif cancellingConnection = YES; @@ -1789,6 +1785,7 @@ static NSComparisonResult _compareFavoritesUsingKey(id favorite1, id favorite2, - (void)_documentWillClose:(NSNotification *)notification { + cancellingConnection = YES; dbDocument = nil; if (mySQLConnection) { [mySQLConnection setDelegate:nil]; diff --git a/Source/SPConnectionHandler.m b/Source/SPConnectionHandler.m index 3495eb95..6efdf366 100644 --- a/Source/SPConnectionHandler.m +++ b/Source/SPConnectionHandler.m @@ -291,8 +291,10 @@ static NSString *SPLocalhostAddress = @"127.0.0.1"; isConnecting = NO; // If the user is only testing the connection, kill the connection - // once established and reset the UI. - if (isTestingConnection) { + // once established and reset the UI. Also catch connection cancels. + if (isTestingConnection || cancellingConnection) { + + // Clean up any connections remaining, and reset the UI [self cancelConnection:self]; if (isTestingConnection) { diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index fa46db2d..7f2abab6 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -415,6 +415,10 @@ static NSString *SPRenameDatabaseAction = @"SPRenameDatabase"; - (void)setConnection:(SPMySQLConnection *)theConnection { + if ([theConnection userTriggeredDisconnect]) { + return; + } + _isConnected = YES; mySQLConnection = [theConnection retain]; @@ -5762,7 +5766,7 @@ static NSString *SPRenameDatabaseAction = @"SPRenameDatabase"; if (processListController) [processListController release]; if (serverVariablesController) [serverVariablesController release]; #endif - if (mySQLConnection) [mySQLConnection release]; + if (mySQLConnection) [mySQLConnection release], mySQLConnection = nil; if (selectedDatabase) [selectedDatabase release]; if (mySQLVersion) [mySQLVersion release]; #ifndef SP_REFACTOR -- cgit v1.2.3