Skip to content

Commit 147f901

Browse files
authored
Added guard clause to stopCapture to prevent incomplete shutdown. (#1833)
* Added guard clause if `stopCapture` is called from the same thread that executes the callbacks. * Added condition to skip throwing an error if stopCapture is called without a started capture.
1 parent b076ef5 commit 147f901

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Pcap++/header/PcapLiveDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ namespace pcpp
499499

500500
/// Stop a currently running packet capture. This method terminates gracefully both packet capture thread and
501501
/// periodic stats collection thread (both if exist)
502+
/// @remarks This method should not be called from the onPacketArrives callback, as it will cause a deadlock.
502503
void stopCapture();
503504

504505
/// Check if a capture thread is running

Pcap++/src/PcapLiveDevice.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ namespace pcpp
873873
if (m_cbOnPacketArrivesBlockingMode != nullptr)
874874
return;
875875

876+
if (m_CaptureThread.get_id() != std::thread::id{} && m_CaptureThread.get_id() == std::this_thread::get_id())
877+
{
878+
throw std::runtime_error("Cannot stop capture from the capture thread itself");
879+
}
880+
876881
m_StopThread = true;
877882
if (m_CaptureThreadStarted)
878883
{

0 commit comments

Comments
 (0)