From 869d766f8dd1460346e908b55a9977221f59a965 Mon Sep 17 00:00:00 2001 From: jack Date: Sun, 4 Jan 2026 14:18:40 -1000 Subject: [PATCH] fix: address race condition in continuation-based waiting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add recheck of message count after acquiring lock and before storing continuation to prevent race where message arrives between initial count check and continuation install. Addresses Codex review feedback on PR #931. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../Fragmentation/FragmentationTests.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bitchatTests/Fragmentation/FragmentationTests.swift b/bitchatTests/Fragmentation/FragmentationTests.swift index 8ba95b4f..e507fe25 100644 --- a/bitchatTests/Fragmentation/FragmentationTests.swift +++ b/bitchatTests/Fragmentation/FragmentationTests.swift @@ -263,6 +263,13 @@ extension FragmentationTests { group.addTask { await withCheckedContinuation { continuation in self.lock.lock() + // Recheck count after acquiring lock to avoid race condition + // where message arrives between initial check and continuation install + if self._publicMessages.count >= count { + self.lock.unlock() + continuation.resume() + return + } self.publicMessageContinuation = continuation self.lock.unlock() } @@ -290,6 +297,13 @@ extension FragmentationTests { group.addTask { await withCheckedContinuation { continuation in self.lock.lock() + // Recheck count after acquiring lock to avoid race condition + // where message arrives between initial check and continuation install + if self._receivedMessages.count >= count { + self.lock.unlock() + continuation.resume() + return + } self.receivedMessageContinuation = continuation self.lock.unlock() }