fix: add thread safety to server status management#1095
fix: add thread safety to server status management#1095Sypher845 wants to merge 2 commits intokrkn-chaos:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds thread safety to the HTTP server's status management by introducing a threading.Lock to protect access to the global server_status variable. The server runs in a separate thread and handles concurrent HTTP requests that read and write the status, making synchronization necessary to prevent race conditions.
Changes:
- Introduced
threadingmodule and created astatus_locklock object - Wrapped all reads and writes to
server_statuswith the lock using context managers - Protected status access in
do_status(),set_run(),set_stop(),set_pause(), andpublish_kraken_status()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tsebastiani
left a comment
There was a problem hiding this comment.
Hey @Sypher845 you have pending discussions with copilot, what say?
|
Hey @tsebastiani , I have already addressed Copilot's suggestions in the latest commit, i forgot to mark resolve them in conversation. |
Signed-off-by: Sypher845 <[email protected]>
Signed-off-by: Sypher845 <[email protected]>
aff619d to
11304da
Compare
|
Also solved the merge conflicts , please review. |
|
Could you kindly provide a screenshot showing server.py running successfully after your modifications, as well as—if available—a screenshot that demonstrates the effectiveness of those changes? |
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||||
|
@tsebastiani I have added the screenshot of the test_server passing successfully in the description( also 5 tests were added to verify thread safety improvements). |
|
@tsebastiani any update on this |
User description
Type of change
Description
Added thread safety to the HTTP server in server.py by introducing a lock mechanism. The server runs in a separate thread and handles requests that read/write a global status variable. Without proper synchronization, this could lead to race conditions if multiple requests happen at the same time.
The fix is simple: added a threading.Lock() and wrapped all access to the server_status variable with it. This ensures only one thread can read or write the status at a time, preventing any potential conflicts.
Related Tickets & Documents
Documentation
Checklist before requesting a review
PR Type
Bug fix, Enhancement
Description
Replaced deprecated
_threadmodule with modernthreadingmoduleAdded thread-safe locking mechanism for
server_statusvariable accessRefactored thread creation to use
threading.Threadwith daemon modeAdded comprehensive test suite for concurrent access and thread safety
Tests after the modification
There were 24 tests before , 5 tests were added to verify thread safety improvements.
Also when we run kraken
Diagram Walkthrough
File Walkthrough
server.py
Modernize threading and add status lock protectionserver.py
import _threadwithimport threadingfor modern threadhandling
status_lock = threading.Lock()to protectserver_statusvariableserver_statusreads and writes with lock context managersstart_server()to usethreading.Threadwith daemon modeinstead of
_thread.start_new_thread()test_server.py
Update tests for threading modernization and add thread safety teststests/test_server.py
threadingandtimemodules@patch('server._thread')to@patch('server.threading.Thread')threading.ThreadAPITestThreadSafetytest class with 5 comprehensive concurrentaccess tests
updates, partial write prevention, and lock safety