Skip to content

network audit fixes#47

Merged
bretth18 merged 2 commits into
mainfrom
b/network-audit-fixes
Jun 9, 2026
Merged

network audit fixes#47
bretth18 merged 2 commits into
mainfrom
b/network-audit-fixes

Conversation

@bretth18

@bretth18 bretth18 commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Description

This PR introduces several improvements and fixes to the peer-to-peer connection and file transfer logic, focusing on race condition prevention, robust timeout handling, and more reliable data buffering. The changes ensure that connection attempts are properly tracked, timeouts reliably cancel pending operations, and file transfer buffers are used consistently to avoid data loss or duplication.

Connection attempt tracking and race condition prevention:

  • Added a generation counter (connectAttemptGeneration) to uniquely identify each connection attempt, ensuring that only the latest attempt can update state or resume continuations, preventing race conditions when multiple attempts overlap. (PeerConnection.swift) [1] [2] [3] [4] [5] [6]

Timeout and cancellation handling:

  • Improved timeout handling by ensuring that a timed-out receive operation cancels the underlying NWConnection, preventing orphaned continuations and guaranteeing that timeouts are honored. (PeerConnection.swift, FileConnectionAttempt.swift) [1] [2] [3]
  • Refactored the connect logic to use a shared claimResume closure and a mutex-protected timeout task, ensuring only one outcome is processed and resources are cleaned up correctly. (FileConnectionAttempt.swift)

File transfer buffer and receive logic:

  • Refactored file transfer receive logic to always drain the buffer before issuing new receives, and to wait for any in-flight message loop receives to finish before proceeding, ensuring data is read in order and without loss. (PeerConnection.swift) [1] [2] [3]
  • Added helper methods and state to track when the message loop has an in-flight receive, and to allow raw readers to wait until it completes before issuing their own receives. (PeerConnection.swift)

Thread safety and state tracking:

  • Changed lastActivityAt to be mutex-protected and accessible from nonisolated contexts, improving thread safety for activity tracking. (PeerConnection.swift)

Robustness and cleanup:

  • Ensured that connections are always cancelled on failure to prevent resource leaks, and that event streams are properly finished if a connection cannot be established. (PeerConnection.swift, FileConnectionAttempt.swift) [1] [2]

These changes collectively make connection management and file transfers more robust, race-free, and reliable under concurrent and error conditions.

How to test

  • Ensure all checks pass

Author checklist

This PR:

  • Satisfies a goal that is specific & clearly motivated
  • Adds value in isolation (whether user-facing or sustainability-related)
  • Contains a concise & easy-to-understand title + description
  • Adheres to SRP by default
  • Presents the best possible implementation to meet its goal, given constraints at hand

bretth18 added 2 commits June 9, 2026 16:18
… races

  - downloads: wedge-forever paths (inbound-F catch, missing stall
    timeout), watchdog token collisions, duplicate queueing, retry
    classifier misclassifying our own teardown errors
  - cancellation: cancelDownload/cancelUpload APIs wired from the UI;
    streaming loops check per chunk, completion can't stomp .cancelled
  - peer connections: double-resume crash on bind retry (generation-
    stamped attempts), out-of-order file bytes after stopReceiving,
    event-stream/Task leak per failed dial, per-IP slot accounting,
    pool key collisions, obfuscated->raw handoff residue
  - coordination: distributed parent death detection, pending-browse
    leaks, login timeout races, NAT external-port re-advertisement
  - protocol: decompression truncation, compress buffer headroom,
    zero-byte files, deleted broken fileSearchRoom (code 25)
  - uploads: duplicate QueueUpload dedup, PF short-read guard, slot
    double-count, speed limit setting wired, legacy direction=download
    TransferRequests answered, atomic share rescan
@bretth18 bretth18 self-assigned this Jun 9, 2026
@bretth18 bretth18 merged commit 050e622 into main Jun 9, 2026
4 checks passed
@bretth18 bretth18 deleted the b/network-audit-fixes branch June 9, 2026 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant