Skip to content

Commit 0c1a606

Browse files
Fix thread race in test008_pairingAfterCancellation_DeviceAttestationVerification (project-chip#37191)
The attestation delegate is called on some random framework-internal queue, so it setting the boolean could race with the test main body reading the boolean, which could cause TSan failures, as well as outright failures in some cases. Adding a sleep(5) into the delegate callback before setting the boolean made the test fail reliably. The fix is to just use an expectation to track the "has the delegate been called?" state, since that will handle synchronization for us.
1 parent d21aaa5 commit 0c1a606

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/darwin/Framework/CHIPTests/MTRPairingTests.m

+7-6
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,15 @@ - (void)test007_pairingAfterCancellation_FindOperational
480480
- (void)test008_pairingAfterCancellation_DeviceAttestationVerification
481481
{
482482
// Cancel pairing while we are waiting for our client to decide what to do
483-
// with the attestation information we got.
484-
__block BOOL delegateCalled = NO;
485-
__auto_type * attestationDelegate = [[NoOpAttestationDelegate alloc] initWithCallback:^{
486-
delegateCalled = YES;
487-
} blockCommissioning:YES];
483+
// with the attestation information we got. Note that the delegate is
484+
// called on some arbitrary queue, so we need to make sure we wait for it to
485+
// actually be called; we can't just have it set a boolean that we then
486+
// check, because that can race.
487+
XCTestExpectation * expectation = [self expectationWithDescription:@"Attestation delegate called"];
488+
__auto_type * attestationDelegate = [[NoOpAttestationDelegate alloc] initWithExpectation:expectation blockCommissioning:YES];
488489

489490
[self doPairingTestAfterCancellationAtProgress:@"Successfully extended fail-safe timer to handle DA failure" attestationDelegate:attestationDelegate];
490-
XCTAssertTrue(delegateCalled);
491+
[self waitForExpectations:@[ expectation ] timeout:kPairingTimeoutInSeconds];
491492
}
492493

493494
- (void)test009_PairWithReadingEndpointInformation

0 commit comments

Comments
 (0)