Skip to content

Conversation

@Dimi1010
Copy link
Collaborator

Split of #1838

Moves the machinery used when packets are collected on a separate worker thread to internal linkage functions.

@Dimi1010 Dimi1010 marked this pull request as ready for review September 21, 2025 12:49
@Dimi1010 Dimi1010 requested a review from seladb as a code owner September 21, 2025 12:49
@codecov
Copy link

codecov bot commented Sep 21, 2025

Codecov Report

❌ Patch coverage is 86.36364% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.45%. Comparing base (a789e12) to head (dbc933d).
⚠️ Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
Pcap++/src/PcapLiveDevice.cpp 86.36% 4 Missing and 5 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1971   +/-   ##
=======================================
  Coverage   83.45%   83.45%           
=======================================
  Files         311      311           
  Lines       54556    54560    +4     
  Branches    11491    11529   +38     
=======================================
+ Hits        45529    45535    +6     
+ Misses       7798     7797    -1     
+ Partials     1229     1228    -1     
Flag Coverage Δ
alpine320 75.89% <68.18%> (-0.02%) ⬇️
fedora42 75.45% <65.85%> (-0.01%) ⬇️
macos-14 81.58% <78.18%> (+<0.01%) ⬆️
macos-15 81.57% <78.18%> (+<0.01%) ⬆️
mingw32 69.98% <63.41%> (-0.04%) ⬇️
mingw64 69.99% <63.41%> (+0.10%) ⬆️
npcap ?
rhel94 75.45% <65.85%> (-0.01%) ⬇️
ubuntu2004 59.46% <65.85%> (-0.03%) ⬇️
ubuntu2004-zstd 59.57% <65.85%> (-0.02%) ⬇️
ubuntu2204 75.41% <65.85%> (-0.02%) ⬇️
ubuntu2204-icpx 57.85% <65.45%> (+<0.01%) ⬆️
ubuntu2404 75.51% <65.85%> (-0.01%) ⬇️
ubuntu2404-arm64 75.56% <68.18%> (-0.01%) ⬇️
unittest 83.45% <86.36%> (+<0.01%) ⬆️
windows-2022 85.41% <84.00%> (+0.16%) ⬆️
windows-2025 85.44% <84.31%> (+0.11%) ⬆️
winpcap 85.44% <84.31%> (-0.09%) ⬇️
xdp 52.99% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 311 to 318
catch (const std::exception& ex)
{
PCPP_LOG_ERROR("Exception occurred while invoking packet arrival callback: " << ex.what());
}
catch (...)
{
PCPP_LOG_ERROR("Unknown exception occurred while invoking packet arrival callback");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should return when an exception is thrown?
Exceptions slow down high performance applications like this...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The catch block logs an error and essentially terminates the hander's current context as there isn't any other code after it. Terminating the entire entire receive loop would be wrong as the error is only local to the currently received packet.

As far as I know, in most implementations try-catch blocks have little to no performance impact if no exception happens.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1971 (comment) also applies.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that we shouldn't catch the exception in onPacketArrivesCallback but rather in captureThreadMain after calling pcap_dispatch, and terminate the capture thread. The reason is that catching exceptions slows down the packet capture. We can let the users catch and handle exceptions in context->callback if they choose to

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that we shouldn't catch the exception in onPacketArrivesCallback but rather in captureThreadMain after calling pcap_dispatch, and terminate the capture thread.

#1971 (comment)

The referenced issue overrides that. We are crossing a C to C++ function boundary as the callback pointer is invoked from C code.

We have to catch the exception before returning from the handler. The C compiled code might or might not know what to do with a thrown exception, but ultimately, we have no guarantee, so it's unsafe.

What we do with the exception after catching is irrelevant.

The reason is that catching exceptions slows down the packet capture. We can let the users catch and handle exceptions in context->callback if they choose to

I would argue that stopping the capture thread on the first encountered exception in the loop slows down the packet capture even more (to 0).

The nominal fast path case should be that the no exceptions happen / reach outside of the user callback to pcpp lib code.

IMO, if the user callback is throwing exceptions everywhere for some reason, slow capture is still better than no capture.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one that doesn't handle exceptions (which is the current behavior)

By the current behaviour, did you mean the base behaviour or the current PR state?
Coz by not having any issues, I meant the current PR state.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the base behavior (before this PR). We don't handle exceptions there...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does leave the issue of UB as the exception is going through the C code, tho. :/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we can handle that in the second PR, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 6d03945
Added a TODO reminder tho.

Comment on lines 342 to 349
catch (const std::exception& ex)
{
PCPP_LOG_ERROR("Exception occurred while invoking packet arrival callback: " << ex.what());
}
catch (...)
{
PCPP_LOG_ERROR("Unknown exception occurred while invoking packet arrival callback");
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to catch an exception in this case?

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propagating an exception from C++ compiled code to C compiled code is considered unsafe. We have to catch any and all exceptions in the handler before returning.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto - we can catch exceptions in captureThreadMainAccumulator and terminate the thread

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 6d03945

@Dimi1010 Dimi1010 requested a review from seladb October 27, 2025 16:37
@seladb
Copy link
Owner

seladb commented Nov 15, 2025

@Dimi1010 this PR is approved. Should we merge it?

@Dimi1010 Dimi1010 merged commit 8b1a9a5 into seladb:dev Nov 15, 2025
72 of 75 checks passed
@Dimi1010 Dimi1010 deleted the refactor/standalone-capture-callbacks-mt branch November 15, 2025 05:14
@Dimi1010 Dimi1010 restored the refactor/standalone-capture-callbacks-mt branch November 15, 2025 05:52
Dimi1010 added a commit that referenced this pull request Nov 15, 2025
@Dimi1010 Dimi1010 deleted the refactor/standalone-capture-callbacks-mt branch November 16, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants