Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why stopCapture waits for 1 second #1246

Closed
tigercosmos opened this issue Nov 29, 2023 · 2 comments
Closed

why stopCapture waits for 1 second #1246

tigercosmos opened this issue Nov 29, 2023 · 2 comments

Comments

@tigercosmos
Copy link
Collaborator

why do we wait 1 second to stop the thread?
if there is any potential race condition, probably 100ms is enough? or could be even shorter.
However, I feel it's an anti-pattern to use sleep to ensure the behavior is correct.

void PcapLiveDevice::stopCapture()
{
	// in blocking mode stop capture isn't relevant
	if (m_cbOnPacketArrivesBlockingMode != nullptr)
		return;

	m_StopThread = true;
	if (m_CaptureThreadStarted)
	{
		pcap_breakloop(m_PcapDescriptor);
		PCPP_LOG_DEBUG("Stopping capture thread, waiting for it to join...");
		m_CaptureThread.join();
		m_CaptureThreadStarted = false;
		PCPP_LOG_DEBUG("Capture thread stopped for device '" << m_Name << "'");
	}
	PCPP_LOG_DEBUG("Capture thread stopped for device '" << m_Name << "'");
	if (m_StatsThreadStarted)
	{
		PCPP_LOG_DEBUG("Stopping stats thread, waiting for it to join...");
		m_StatsThread.join();
		m_StatsThreadStarted = false;
		PCPP_LOG_DEBUG("Stats thread stopped for device '" << m_Name << "'");
	}

	multiPlatformSleep(1);
	m_StopThread = false;
}

cc @clementperon @seladb

@seladb
Copy link
Owner

seladb commented Nov 29, 2023

@tigercosmos this goes back all the way to the first commit in PcapPlusPlus: 488754e#diff-6dc93f9ea25d0bcd5957051301b157d18c254960c90fdef71e8a819ffb9cb273R281

It was 9 years ago so I don't remember why I added it, but probably to avoid a race condition when joining threads 🤷

@egecetin
Copy link
Collaborator

Fixed with #1247

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

No branches or pull requests

3 participants