Skip to content

fix(pcapwriter): serialize DSB keylog writes and fix race condition in Close()#971

Merged
cfc4n merged 2 commits into
masterfrom
bugfix/pcapwriter-thread-safety
Mar 29, 2026
Merged

fix(pcapwriter): serialize DSB keylog writes and fix race condition in Close()#971
cfc4n merged 2 commits into
masterfrom
bugfix/pcapwriter-thread-safety

Conversation

@cfc4n
Copy link
Copy Markdown
Member

@cfc4n cfc4n commented Mar 29, 2026

  • Serialize WriteKeyLog() through Serve() goroutine via keylogChan to avoid concurrent access to NgWriter (not thread-safe)
  • Add serveDone channel so Close() waits for Serve() to finish before closing channels and flushing, eliminating shutdown race condition
  • Flush pending packets before each DSB write to maintain correct PCAPNG block order (packets → DSB → packets)
  • Drain and save remaining packets when packetChan is closed
  • Add Truncate option to FileWriter; use it in openssl/gotls probes to overwrite stale pcapng files on new capture instead of appending
  • Remove unused masterKeyBuffer, tcPacketLocker fields and imports
  • Clean up stale comments and empty defers in savePcapng()

Open with Devin

Copilot AI review requested due to automatic review settings March 29, 2026 12:08
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. fix bug fix PR labels Mar 29, 2026

This comment was marked as resolved.

@github-actions
Copy link
Copy Markdown

🔧 Debug Build Complete (PR #971)

📦 Download Links:

⏰ Files will be retained for 7 days, please download and test promptly.


This build includes debug binaries for: android/linux (arm64/amd64)

@github-actions
Copy link
Copy Markdown

✅ E2E Test Results: PASSED

Test Run: #23708683640

Tests Executed:

  • TLS/OpenSSL Module (curl → github.com)
  • GnuTLS Module (wget/curl → github.com)
  • GoTLS Module (Go client → github.com)
  • ecaptureQ Module (WebSocket event streaming)

✅ All e2e tests passed successfully! The TLS capture functionality is working correctly.


Automated e2e test results for commit f8392de

devin-ai-integration[bot]

This comment was marked as resolved.

- Serialize WriteKeyLog() through Serve() goroutine via keylogChan to
  avoid concurrent access to NgWriter (not thread-safe)
- Add serveDone channel so Close() waits for Serve() to finish before
  closing channels and flushing, eliminating shutdown race condition
- Drain both packetChan and keylogChan on context cancellation in Serve()
  so queued data is not silently dropped at shutdown
- Set keylogChan to nil on channel close to prevent CPU spin loop
  (receiving from closed channel returns zero value immediately)
- Make Flush() safe for concurrent use: no-op while Serve() is running
  (Serve handles flushing internally), direct flush only after Serve exits
- Remove racy Flush() call in PcapHandler.Close() that accessed NgWriter
  while Serve() was still running; PcapWriter.Close() handles this safely
- Flush pending packets before each DSB write to maintain correct
  PCAPNG block order (packets -> DSB -> packets)
- Add Truncate option to FileWriter; use it in openssl/gotls probes
  to overwrite stale pcapng files on new capture instead of appending
- Remove unused masterKeyBuffer, tcPacketLocker fields and imports
- Clean up stale comments and empty defers in savePcapng()
@cfc4n cfc4n force-pushed the bugfix/pcapwriter-thread-safety branch from c00c77e to a95897b Compare March 29, 2026 12:29
@github-actions
Copy link
Copy Markdown

🔧 Debug Build Complete (PR #971)

📦 Download Links:

⏰ Files will be retained for 7 days, please download and test promptly.


This build includes debug binaries for: android/linux (arm64/amd64)

@github-actions
Copy link
Copy Markdown

✅ E2E Test Results: PASSED

Test Run: #23709062031

Tests Executed:

  • TLS/OpenSSL Module (curl → github.com)
  • GnuTLS Module (wget/curl → github.com)
  • GoTLS Module (Go client → github.com)
  • ecaptureQ Module (WebSocket event streaming)

✅ All e2e tests passed successfully! The TLS capture functionality is working correctly.


Automated e2e test results for commit 0484949

devin-ai-integration[bot]

This comment was marked as resolved.

This comment was marked as resolved.

Root cause: Wireshark/tshark processes pcapng blocks sequentially and
needs the Decryption Secrets Block (DSB) to appear BEFORE the encrypted
packet blocks. ecapture was writing packets first, then DSB, placing
the keylog after all packets where Wireshark couldn't use it.

Verified: the DSB content was correct (format, secrets_type, keylog
data all valid), but its position in the file made it invisible to
Wireshark's TLS dissector. The same keylog extracted and passed via
tls.keylog_file worked perfectly.

Changes:
- Reverse DSB/packet ordering in Serve(): write DSB first, then flush
  buffered packets, so DSB precedes packets in the pcapng file
- Add dsbGraceDeadline (10s): delay the first timer-based packet flush
  until the first DSB arrives, preventing the timer from flushing
  packets to disk before the keylog has been captured
- Add firstDSBWritten flag to track DSB state for the grace period
- Fix drainOnShutdown(): write DSBs before packets on shutdown too
@github-actions
Copy link
Copy Markdown

🔧 Debug Build Complete (PR #971)

📦 Download Links:

⏰ Files will be retained for 7 days, please download and test promptly.


This build includes debug binaries for: android/linux (arm64/amd64)

@github-actions
Copy link
Copy Markdown

✅ E2E Test Results: PASSED

Test Run: #23709723072

Tests Executed:

  • TLS/OpenSSL Module (curl → github.com)
  • GnuTLS Module (wget/curl → github.com)
  • GoTLS Module (Go client → github.com)
  • ecaptureQ Module (WebSocket event streaming)

✅ All e2e tests passed successfully! The TLS capture functionality is working correctly.


Automated e2e test results for commit bf85b9b

@cfc4n cfc4n merged commit 747052f into master Mar 29, 2026
11 of 12 checks passed
@cfc4n cfc4n deleted the bugfix/pcapwriter-thread-safety branch March 29, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix bug fix PR size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants