Skip to content

Commit

Permalink
Fix thread race in test008_pairingAfterCancellation_DeviceAttestation…
Browse files Browse the repository at this point in the history
…Verification (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.
  • Loading branch information
bzbarsky-apple authored Jan 27, 2025
1 parent d21aaa5 commit 0c1a606
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/darwin/Framework/CHIPTests/MTRPairingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,15 @@ - (void)test007_pairingAfterCancellation_FindOperational
- (void)test008_pairingAfterCancellation_DeviceAttestationVerification
{
// Cancel pairing while we are waiting for our client to decide what to do
// with the attestation information we got.
__block BOOL delegateCalled = NO;
__auto_type * attestationDelegate = [[NoOpAttestationDelegate alloc] initWithCallback:^{
delegateCalled = YES;
} blockCommissioning:YES];
// with the attestation information we got. Note that the delegate is
// called on some arbitrary queue, so we need to make sure we wait for it to
// actually be called; we can't just have it set a boolean that we then
// check, because that can race.
XCTestExpectation * expectation = [self expectationWithDescription:@"Attestation delegate called"];
__auto_type * attestationDelegate = [[NoOpAttestationDelegate alloc] initWithExpectation:expectation blockCommissioning:YES];

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

- (void)test009_PairWithReadingEndpointInformation
Expand Down

0 comments on commit 0c1a606

Please sign in to comment.