Skip to content
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

Set tube->ev_listen to NULL to prevent double unregister in winsock code path #774

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jagerman
Copy link

@jagerman jagerman commented Nov 1, 2022

On windows when using threaded mode (i.e. ub_ctx_async(ctx, 1)) tube_remove_bg_listen gets called twice during a ub_ctx_delete: once when the thread does its own cleanup, then again in tube_delete(). Because ev_listen doesn't get cleared, however, we end we calling ub_winsock_unregister_wsaevent() with a freed pointer on the second call.

This doesn't always manifest because, apparently, for various compilers and settings that memory might be overwritten in which case the additional check for ev->magic will prevent anything actually happening, but in my case under mingw32 that doesn't happen and we end up crashing (but frustratingly does get changed under gdb, which made this rather annoying to track down).

This fixes the crash by properly NULLing the pointer so that the second ub_winsock_unregister_wsaevent(...) becomes a no-op.

(Setting it to null also matches what the non-winsock version of the code does).

On windows when using threaded mode (i.e. `ub_ctx_async(ctx, 1)`)
tube_remove_bg_listen gets called twice: once when the thread does its
own cleanup, then again in `tube_delete()`.  Because `ev_listen` doesn't
get cleared, however, we end we calling ub_winsock_unregister_wsaevent
with a freed pointer.

This doesn't always manifest because, apparently, for various compilers
and settings that memory *might* be overwritten in which case the
additional check for ev->magic will prevent anything actually happening,
but in my case under mingw32 that doesn't happen and we end up
eventually crashing.

This fixes the crash by properly NULLing the pointer so that the second
ub_winsock_unregister_wsaevent(...) becomes a no-op.
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.

1 participant