-
Notifications
You must be signed in to change notification settings - Fork 29
issue: 4432879 Refactor event_handler_manager #245
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
base: vNext
Are you sure you want to change the base?
Conversation
|
@pasis, can we keep this in your privare fork? |
|
bot:retest |
|
bot:retest |
1 similar comment
|
bot:retest |
|
/review |
|
/improve |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨
|
|
Hi @pasis , @AlexanderGrissik , please check pr agent review comments. |
|
bot:retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Greptile Summary
Refactors event handler manager initialization to start the thread once during XLIO initialization instead of on every registration action.
Key changes:
- Moved
start_thread()call frompost_new_reg_action()todo_global_ctors_helper()inmain.cpp - Made
start_thread()public to enable explicit initialization control - Removed
free_evh_resources()method, inlining cleanup logic into destructor - Simplified constructor by setting
m_b_continue_running = trueunconditionally before mode check - Improved error handling in
start_thread()with better logging and explicit error returns
Benefits:
- Eliminates redundant thread creation checks on every registration (CPS performance improvement)
- Cleaner separation of concerns - initialization vs. registration
- More predictable thread lifecycle management
Critical issue found:
- Line 338-339 in
event_handler_manager.cpp: Logic error in fallback path wherepthread_createmay be called with uninitialized attributes if secondpthread_attr_initfails
Confidence Score: 2/5
- This PR contains a critical logic bug that must be fixed before merging
- The refactoring concept is sound and improves performance, but the retry logic in
start_thread()(lines 338-339) has a critical bug wherepthread_createcan be called with an uninitializedtattrif the secondpthread_attr_initfails. This could cause undefined behavior or crashes during thread creation fallback scenarios. - src/core/event/event_handler_manager.cpp (lines 338-339) - fix the compound statement logic error
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| src/core/event/event_handler_manager.cpp | 2/5 | Refactored start_thread with improved error handling but has critical logic bug in retry path (line 338-339) |
| src/core/event/event_handler_manager.h | 5/5 | Made start_thread() public and removed free_evh_resources() declaration - clean refactoring |
| src/core/main.cpp | 5/5 | Added explicit start_thread() call with error handling during initialization - improves control flow |
Sequence Diagram
sequenceDiagram
participant Main as main.cpp
participant EHM as event_handler_manager
participant Thread as event_handler_thread
participant Reg as Registration callers
Note over Main: XLIO initialization (do_global_ctors_helper)
Main->>EHM: NEW_CTOR(g_p_event_handler_manager, event_handler_manager())
activate EHM
EHM->>EHM: m_b_continue_running = true
EHM->>EHM: epoll_create(m_epfd)
EHM->>EHM: wakeup_set_epoll_fd(m_epfd)
EHM-->>Main: return
deactivate EHM
Main->>EHM: start_thread()
activate EHM
EHM->>EHM: pthread_attr_init(&tattr)
EHM->>EHM: pthread_attr_setaffinity_np (optional)
EHM->>Thread: pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this)
activate Thread
EHM-->>Main: return 0 (success)
deactivate EHM
Note over Main: Continue with initialization<br/>Create g_p_ib_ctx_handler_collection,<br/>g_p_net_device_table_mgr, etc.
par Event Handler Thread Loop
Thread->>Thread: thread_loop()
loop while m_b_continue_running
Thread->>Thread: m_timer.update_timeout()
Thread->>Thread: epoll_wait(m_epfd, ...)
Thread->>Thread: process registration actions
Thread->>Thread: process events
end
and Registration Actions (from other components)
Reg->>EHM: register_ibverbs_event()
activate EHM
EHM->>EHM: post_new_reg_action(reg_action)
Note over EHM: OLD: Called start_thread() here<br/>NEW: Thread already running
EHM->>EHM: m_p_reg_action_q_to_push_to->push_back()
EHM->>EHM: do_wakeup()
EHM-->>Reg: return
deactivate EHM
EHM-->>Thread: wakeup signal
Thread->>Thread: handle_registration_action()
end
Note over Main: Shutdown sequence
Main->>EHM: ~event_handler_manager()
activate EHM
EHM->>EHM: stop_thread()
EHM->>EHM: m_b_continue_running = false
EHM->>Thread: do_wakeup()
Thread->>Thread: exit thread_loop()
Thread->>Thread: process_remaining_registration_actions()
deactivate Thread
EHM->>EHM: pthread_join(m_event_handler_tid)
EHM->>EHM: close(m_epfd)
deactivate EHM
3 files reviewed, 1 comment
| ret = pthread_attr_init(&tattr) | ||
| ?: pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: logic error with ternary operator: if pthread_attr_init fails, pthread_create still executes with potentially uninitialized tattr
| ret = pthread_attr_init(&tattr) | |
| ?: pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this); | |
| if (pthread_attr_init(&tattr) != 0) { | |
| ret = -1; | |
| } else { | |
| ret = pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this); | |
| } |
Event handler manager is a global object with a single internal thread. Once XLIO is initialized, there is no option not to start the internal thread and it's used starting from the initialization method (to create a timer event for netlink). Therefore, there is no point in trying to start the thread with each post action to the event handler. This can save CPU in CPS scenario. Signed-off-by: Dmytro Podgornyi <[email protected]>
This method simply calls stop_thread(). Remove it to simply the code and separate thread stopping from epfd closing. Signed-off-by: Dmytro Podgornyi <[email protected]>
Greptile Summary
Confidence Score: 3/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant "do_global_ctors_helper" as Init
participant "event_handler_manager" as EHM
participant "pthread" as Thread
User->>Init: "Initialize XLIO"
Init->>EHM: "NEW_CTOR(g_p_event_handler_manager, event_handler_manager())"
activate EHM
EHM->>EHM: "m_b_continue_running = true"
EHM->>EHM: "epoll_create()"
EHM->>EHM: "wakeup_set_epoll_fd()"
EHM-->>Init: "Return manager instance"
deactivate EHM
Init->>EHM: "start_thread()"
activate EHM
EHM->>EHM: "Check m_b_continue_running"
EHM->>EHM: "Check m_event_handler_tid == 0"
EHM->>EHM: "pthread_attr_init(&tattr)"
EHM->>EHM: "Set affinity if requested"
EHM->>Thread: "pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this)"
Thread-->>EHM: "Return thread ID"
EHM->>EHM: "pthread_attr_destroy(&tattr)"
EHM-->>Init: "Return 0 on success"
deactivate EHM
Init->>Init: "Check rc != 0"
Init->>User: "Throw exception if failed"
Note over EHM,Thread: "Thread now runs continuously<br/>handling events via thread_loop()"
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, no comments
Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format
Description
Avoid start_thread with each reg action.
Refactor destructor.
What
Avoid start_thread with each reg action.
Refactor destructor.
Why ?
Potential CPU improvement for CPS and code cleanup.
Change type
What kind of change does this PR introduce?
Check list