Skip to content

fix wrong static declaration in pqithreadstreamer.cc#296

Merged
csoler merged 1 commit into
RetroShare:masterfrom
jolavillette:FixThreadStreamer
May 15, 2026
Merged

fix wrong static declaration in pqithreadstreamer.cc#296
csoler merged 1 commit into
RetroShare:masterfrom
jolavillette:FixThreadStreamer

Conversation

@jolavillette
Copy link
Copy Markdown
Contributor

fix wrong static declaration in pqithreadstreamer.cc

This branch fixed a critical bug in the pqithreadstreamer class related to how thread sleep and timeout periods were managed.

The Problem
In the original code, the variables recv_timeout and sleep_period inside the threadTick() function were declared as static:
This bug was introduced by me (jolavillette) in
commit 2e2bcb8
Author: jolavillette jolavillette@gmail.com
Date: Fri Jan 2 22:17:30 2026 +0100
use adaptive timeout and sleep in pqithreadstreamer

cpp
static uint32_t recv_timeout = mTimeout;
static uint32_t sleep_period = mSleepPeriod;
In C++, a static variable inside a function is shared across all instances of that class. Since pqithreadstreamer is instantiated for every single peer connection (e.g., if you have 50 friends online, you have 50 instances), they were all fighting over the same two variables.

This created several issues:

  • Cross-connection interference: Activity on one peer connection would reset the "global" timeout/sleep for all other connections.
    Incorrect adaptation: The adaptive logic (which reduces sleep when there is data and increases it when idle) couldn't work properly because it was being updated by multiple threads simultaneously without coordination.
  • Performance & CPU usage: Idle connections might not sleep enough if another connection was busy, leading to unnecessary CPU consumption.

The Fix
The branch removed the static keywords and used instance member variables (mTimeout and mSleepPeriod) instead.

Now, each peer connection has its own independent timing state. This ensures that:

  • Adaptive timing works correctly for each individual connection.
  • Idle connections sleep properly to save CPU, regardless of what other peers are doing.
  • Data races and state pollution between peer threads are eliminated.

@csoler csoler merged commit de22910 into RetroShare:master May 15, 2026
1 check failed
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.

2 participants