@@ -520,6 +520,7 @@ void SetUpCodePairer::OnBLEDiscoveryError(CHIP_ERROR err)
520520 ChipLogError (Controller, " Commissionable node discovery over BLE failed: %" CHIP_ERROR_FORMAT, err.Format ());
521521 mWaitingForDiscovery [kBLETransport ] = false ;
522522 LogErrorOnFailure (err);
523+ StopPairingIfTransportsExhausted (err);
523524}
524525#endif // CONFIG_NETWORK_LAYER_BLE
525526
@@ -543,6 +544,7 @@ void SetUpCodePairer::OnWifiPAFDiscoveryError(CHIP_ERROR err)
543544{
544545 ChipLogError (Controller, " Commissionable node discovery over Wi-Fi PAF failed: %" CHIP_ERROR_FORMAT, err.Format ());
545546 mWaitingForDiscovery [kWiFiPAFTransport ] = false ;
547+ StopPairingIfTransportsExhausted (err);
546548}
547549
548550void SetUpCodePairer::OnWiFiPAFSubscribeComplete (void * appState)
@@ -578,6 +580,7 @@ void SetUpCodePairer::OnTagDiscoveryFailed(CHIP_ERROR error)
578580{
579581 ChipLogError (Controller, " Commissionable node discovery over NFC failed: %" CHIP_ERROR_FORMAT, error.Format ());
580582 mWaitingForDiscovery [kNFCTransport ] = false ;
583+ StopPairingIfTransportsExhausted (error);
581584}
582585#endif
583586
@@ -712,6 +715,20 @@ bool SetUpCodePairer::DiscoveryInProgress() const
712715 return false ;
713716}
714717
718+ void SetUpCodePairer::StopPairingIfTransportsExhausted (CHIP_ERROR err)
719+ {
720+ if (mWaitingForPASE || !mDiscoveredParameters .empty () || DiscoveryInProgress () || mRemoteId == kUndefinedNodeId )
721+ {
722+ return ;
723+ }
724+ // Clear mRemoteId first to guard against re-entrant calls (e.g. from an async
725+ // cancel callback fired after StopAllDiscoveryAttempts already cleared the flags).
726+ mRemoteId = kUndefinedNodeId ;
727+ CHIP_ERROR failErr = mLastPASEError != CHIP_NO_ERROR ? mLastPASEError : err;
728+ MATTER_LOG_METRIC_END (kMetricSetupCodePairerPairDevice , failErr);
729+ mCommissioner ->OnSessionEstablishmentError (failErr);
730+ }
731+
715732void SetUpCodePairer::StopAllDiscoveryAttempts ()
716733{
717734 LogErrorOnFailure (StopDiscoveryOverBLE ());
@@ -834,6 +851,16 @@ void SetUpCodePairer::OnPairingComplete(CHIP_ERROR error, const std::optional<Re
834851 ChipLogError (Controller, " Error when verifying the validity of an address: %" CHIP_ERROR_FORMAT, err.Format ());
835852 }
836853 }
854+ // If this was a DNS-SD-triggered (IP/UDP) PASE attempt, stop DNS-SD now.
855+ // All addresses for this device were already queued when DNS-SD found the
856+ // record, so continued DNS-SD scanning cannot help. More importantly,
857+ // leaving DNS-SD running would keep DiscoveryInProgress() true indefinitely,
858+ // which would prevent StopPairingIfTransportsExhausted from ever firing once
859+ // the physical-proximity transports (BLE, Wi-Fi PAF, NFC) also complete.
860+ if (mCurrentPASEParameters .HasValue ())
861+ {
862+ LogErrorOnFailure (StopDiscoveryOverDNSSD ());
863+ }
837864 mCurrentPASEParameters .ClearValue ();
838865
839866 // We failed to establish PASE. Try the next thing we have discovered, if
@@ -873,20 +900,19 @@ void SetUpCodePairer::OnDeviceDiscoveredTimeoutCallback(System::Layer * layer, v
873900{
874901 ChipLogError (Controller, " Discovery timed out" );
875902 auto * pairer = static_cast <SetUpCodePairer *>(context);
876- pairer->StopAllDiscoveryAttempts ();
877- if (!pairer->mWaitingForPASE && pairer->mDiscoveredParameters .empty ())
903+
904+ // If a PASE attempt is in progress, do not stop any discovery — give every
905+ // transport that has started (DNS-SD, BLE, Wi-Fi PAF, NFC) a chance to
906+ // complete, fail, or time out naturally. DNS-SD will be stopped in
907+ // OnPairingComplete once the PASE attempt it triggered completes.
908+ if (pairer->mWaitingForPASE )
878909 {
879- // We're not waiting on any more PASE attempts, and we're not going to
880- // discover anything at this point, so we should just notify our
881- // listener.
882- CHIP_ERROR err = pairer->mLastPASEError ;
883- if (err == CHIP_NO_ERROR)
884- {
885- err = CHIP_ERROR_TIMEOUT;
886- }
887- MATTER_LOG_METRIC_END (kMetricSetupCodePairerPairDevice , err);
888- pairer->mCommissioner ->OnSessionEstablishmentError (err);
910+ return ;
889911 }
912+
913+ // No PASE in progress — stop all remaining discovery and fail if nothing is left to try.
914+ pairer->StopAllDiscoveryAttempts ();
915+ pairer->StopPairingIfTransportsExhausted (CHIP_ERROR_TIMEOUT);
890916}
891917
892918bool SetUpCodePairer::ShouldDiscoverUsing (RendezvousInformationFlag commissioningChannel) const
0 commit comments