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

Resolve ipc race condition #212

Merged
merged 1 commit into from
Jun 7, 2024
Merged
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
2 changes: 1 addition & 1 deletion Example/SBTUITestTunnel_Tests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1</string>
<key>SBTUITestTunnelDisableIPC</key>
<true/>
<false/>
</dict>
</plist>
9 changes: 9 additions & 0 deletions Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ - (void)waitForConnection

- (void)serverDidConnect:(id)sender
{
while (!self.ipcConnection.isValid) {
if (CFAbsoluteTimeGetCurrent() - self.launchStart > SBTUITunneledApplicationDefaultTimeout) {
[self shutDownWithErrorMessage:@"[SBTUITestTunnel] IPC tunnel did fail to connect" code:SBTUITestTunnelErrorConnectionToApplicationFailed];
return;
}

[NSRunLoop.mainRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
}

__weak typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.connected = YES;
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
18 changes: 14 additions & 4 deletions Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ - (NSMethodSignature *)protocolMethodSignatureForSelector:(SEL)aSelector
@implementation DTXIPCConnection
{
dispatch_queue_t _dispatchQueue;
dispatch_queue_t _otherConnectionQueue;

NSRunLoop* _runLoop;
NSString* _actualServiceName;

Expand Down Expand Up @@ -224,7 +226,8 @@ - (instancetype)initWithServiceName:(NSString *)serviceName
_slave = NO;

_dispatchQueue = dispatch_queue_create([NSString stringWithFormat:@"com.wix.DTXIPCConnection:%@", _serviceName].UTF8String, dispatch_queue_attr_make_with_autorelease_frequency(NULL, DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM));

_otherConnectionQueue = dispatch_queue_create([NSString stringWithFormat:@"com.wix.DTXIPCOtherConnection:%@", _serviceName].UTF8String, NULL);

//Attempt becoming a master
if([self _commonInit] == NO)
{
Expand Down Expand Up @@ -294,7 +297,12 @@ - (void)invalidate

- (BOOL)isValid
{
return _connection.isValid && _otherConnection.isValid;
__block BOOL ret = NO;
dispatch_sync(_otherConnectionQueue, ^{
ret = _connection.isValid && _otherConnection.isValid;
});

return ret;
}

- (id)remoteObjectProxy
Expand Down Expand Up @@ -323,8 +331,10 @@ - (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_sync(_otherConnectionQueue, ^{
_otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:_otherConnection];
});
}

- (oneway void)_invokeFromRemote:(NSDictionary*)serializedInvocation
Expand Down
1 change: 1 addition & 0 deletions Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ - (BOOL)takeOffOnce
- (BOOL)takeOffOnceIPCWithServiceIdentifier:(NSString *)serviceIdentifier
{
self.ipcConnection = [[DTXIPCConnection alloc] initWithServiceName:[NSString stringWithFormat:@"com.subito.sbtuitesttunnel.ipc.%@", serviceIdentifier]];

self.ipcConnection.remoteObjectInterface = [DTXIPCInterface interfaceWithProtocol:@protocol(SBTIPCTunnel)];
self.ipcConnection.exportedInterface = [DTXIPCInterface interfaceWithProtocol:@protocol(SBTIPCTunnel)];
self.ipcConnection.exportedObject = self;
Expand Down
Loading