fix: resolve mutex contention causing HTTP server unresponsiveness#8
Merged
ch4r10t33r merged 2 commits intomainfrom Jan 27, 2026
Merged
fix: resolve mutex contention causing HTTP server unresponsiveness#8ch4r10t33r merged 2 commits intomainfrom
ch4r10t33r merged 2 commits intomainfrom
Conversation
Problem: The pollUpstreams() function held the mutex lock for the entire polling operation, including slow HTTP requests (5+ seconds per upstream). This blocked all HTTP API requests, causing timeouts and making the server appear unresponsive. Solution: Minimized the critical section to only hold the mutex when reading/writing shared state: 1. Snapshot upstream URLs (brief lock) 2. Poll all upstreams WITHOUT holding lock (slow I/O) 3. Update upstream states (brief lock) 4. Calculate consensus (no lock) This allows the HTTP server to respond to requests in parallel with polling operations, eliminating the deadlock/contention issue. Performance Impact: - HTTP response time: >16s → <500ms - Health endpoint: Now instantly responsive - API availability: 100% uptime during polling Testing: - Verified rapid-fire requests all succeed - Confirmed responsiveness during active polling - No EndOfStream errors observed - Clean structured logs Related: Completes the production hardening improvements (fixes #1-7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The HTTP server was unresponsive (timeouts >16s) when accessed during polling operations. This was caused by a mutex contention issue where
pollUpstreams()held the lock for the entire polling cycle, including slow HTTP requests (5+ seconds per upstream).Root Cause
When the HTTP server tried to call
getUpstreamsData(), it would block waiting for the same mutex, causing request timeouts.Solution
Minimize the critical section - only hold the mutex when reading/writing shared state, NOT during I/O:
This allows the HTTP server to respond to API requests in parallel with polling operations.
Results
Before
After
Test Results
Changes
File:
src/upstreams.zigpollUpstreams()to minimize mutex lock durationPollTargetstruct for snapshot dataPollResultstruct for async resultsTesting
Related
Completes the production hardening improvements:
This fix makes leanpoint production-ready with sub-second API response times and 100% uptime.