Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/native/eventpipe/ep-buffer-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ buffer_manager_try_convert_buffer_to_read_only (
EventPipeThread *thread = ep_buffer_get_writer_thread (new_read_buffer);
EP_SPIN_LOCK_ENTER (ep_thread_get_rt_lock_ref (thread), section1);
EventPipeThreadSessionState *thread_session_state = ep_thread_get_session_state (thread, buffer_manager->session);
EP_ASSERT(thread_session_state != NULL);
if (ep_thread_session_state_get_write_buffer (thread_session_state) == new_read_buffer) {
ep_thread_session_state_set_write_buffer (thread_session_state, NULL);
EP_ASSERT (ep_buffer_get_volatile_state (new_read_buffer) == EP_BUFFER_STATE_READ_ONLY);
Expand Down Expand Up @@ -1091,6 +1092,7 @@ ep_buffer_manager_suspend_write_event (
EventPipeThread *thread = ep_rt_thread_array_iterator_value (&thread_array_iterator);
EP_SPIN_LOCK_ENTER (ep_thread_get_rt_lock_ref (thread), section2)
EventPipeThreadSessionState *thread_session_state = ep_thread_get_session_state (thread, buffer_manager->session);
EP_ASSERT(thread_session_state != NULL);
ep_thread_session_state_set_write_buffer (thread_session_state, NULL);
EP_SPIN_LOCK_EXIT (ep_thread_get_rt_lock_ref (thread), section2)
ep_rt_thread_array_iterator_next (&thread_array_iterator);
Expand Down
46 changes: 46 additions & 0 deletions src/native/eventpipe/ep-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ static
void
session_create_streaming_thread (EventPipeSession *session);

static
void
ep_session_remove_dangling_session_states (EventPipeSession *session);

/*
* EventPipeSession.
*/
Expand Down Expand Up @@ -215,6 +219,46 @@ ep_session_alloc (
ep_exit_error_handler ();
}

void
ep_session_remove_dangling_session_states (EventPipeSession *session)
{
ep_return_void_if_nok (session != NULL);

EP_RT_DECLARE_LOCAL_THREAD_ARRAY (threads);
ep_rt_thread_array_init (&threads);

ep_thread_get_threads (&threads);

ep_rt_thread_array_iterator_t threads_iterator = ep_rt_thread_array_iterator_begin (&threads);
while (!ep_rt_thread_array_iterator_end (&threads, &threads_iterator)) {
EventPipeThread *thread = ep_rt_thread_array_iterator_value (&threads_iterator);
EP_ASSERT(thread != NULL);
EP_SPIN_LOCK_ENTER (ep_thread_get_rt_lock_ref (thread), section1);
EventPipeThreadSessionState *session_state = ep_thread_get_session_state(thread, session);
if (session_state) {
// If a buffer tries to write event(s) but never gets a buffer because the maximum total buffer size
// has been exceeded, we can leak the EventPipeThreadSessionState* and crash later trying to access
// the session from the thread session state. Whenever we terminate a session we check to make sure
// we haven't leaked any thread session states.
ep_thread_delete_session_state(thread, session);
}
EP_SPIN_LOCK_EXIT (ep_thread_get_rt_lock_ref (thread), section1);

// ep_thread_get_threads calls ep_thread_addref for every entry, need to release it here
ep_thread_release (thread);

ep_rt_thread_array_iterator_next (&threads_iterator);
}

ep_rt_thread_array_fini (&threads);

ep_on_exit:
return;

ep_on_error:
ep_exit_error_handler ();
}

void
ep_session_free (EventPipeSession *session)
{
Expand All @@ -229,6 +273,8 @@ ep_session_free (EventPipeSession *session)
ep_buffer_manager_free (session->buffer_manager);
ep_file_free (session->file);

ep_session_remove_dangling_session_states (session);

ep_rt_object_free (session);
}

Expand Down
1 change: 0 additions & 1 deletion src/native/eventpipe/ep-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ ep_thread_get_session_state (

ep_thread_requires_lock_held (thread);

EP_ASSERT (thread->session_state [ep_session_get_index (session)] != NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you hit this assert? Looking at the callers of ep_thread_get_session_state, you should end up with more asserts in the case this returns NULL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in ep_session_remove_dangling_session_states iterates over all threads and under normal circumstances it will return NULL, it will only return non-NULL if a session state is leaked. So we will hit this assert in the code I added

Copy link
Member

@lateralusX lateralusX Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I didn't find your call since I didn't search your branch for callers of ep_thread_get_session_state and for the other callers it is critical that this assert holds. Maybe we could move the assert to the callers (only two places) where it should still be true, ep_thread_get_session_state shouldn't return a NULL session state.

return thread->session_state [ep_session_get_index (session)];
}

Expand Down