Skip to content
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

Fix threading issue in IPC connection #210

Closed
Closed
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
21 changes: 16 additions & 5 deletions Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,26 @@ - (void)serverDidConnect:(id)sender

NSLog(@"[SBTUITestTunnel] IPC tunnel did connect after, %fs", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart);

if (weakSelf.startupBlock) {
weakSelf.startupBlock();
[weakSelf checkConnectionAndProceed];
});
}

- (void)checkConnectionAndProceed
{
if (self.ipcConnection.isValid) {
if (_startupBlock) {
_startupBlock();
NSLog(@"[SBTUITestTunnel] Did perform startupBlock");
}

weakSelf.startupCompleted = [[weakSelf sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStartupCommandsCompleted params:@{}] isEqualToString:@"YES"];
_startupCompleted = [[self sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStartupCommandsCompleted params:@{}] isEqualToString:@"YES"];

NSLog(@"[SBTUITestTunnel] Tunnel ready after %fs", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart);
});
NSLog(@"[SBTUITestTunnel] Tunnel ready after %fs", CFAbsoluteTimeGetCurrent() - _launchStart);
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self checkConnectionAndProceed];
});
}
}

- (void)performCommandWithParameters:(NSDictionary *)parameters block:(void (^)(NSDictionary *))block {}
Expand Down
2 changes: 2 additions & 0 deletions Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ NS_ASSUME_NONNULL_BEGIN
/// Invalidate the connection. All outstanding error handling blocks will be called on the message handling queue. The connection must be invalidated before it is deallocated. After a connection is invalidated, no more messages may be sent or received.
- (void)invalidate;

- (BOOL)isValid;

@end

NS_ASSUME_NONNULL_END
12 changes: 10 additions & 2 deletions Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,16 @@ - (void)setExportedObject:(id)exportedObject

- (oneway void)_slaveDidConnectWithName:(NSString*)slaveServiceName
{
_otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:_otherConnection];
dispatch_block_t block = ^{
self->_otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:self->_otherConnection];
};

if ([NSThread isMainThread]) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}
}

- (oneway void)_invokeFromRemote:(NSDictionary*)serializedInvocation
Expand Down
Loading