Skip to content

osx,fsevent: fix race during uv_loop_close#2626

Merged
vtjnash merged 2 commits intolibuv:v1.xfrom
vtjnash:jn/2625
Jan 21, 2020
Merged

osx,fsevent: fix race during uv_loop_close#2626
vtjnash merged 2 commits intolibuv:v1.xfrom
vtjnash:jn/2625

Conversation

@vtjnash
Copy link
Copy Markdown
Member

@vtjnash vtjnash commented Jan 13, 2020

The mutex also needs to protect the access to the state->loop variable,
since that's owned by the child thread and will be destroyed as soon as
it processes our message.

This previously caused shutdown of libuv loops (especially the stress test fs_event_error_reporting) to segfault occasionally.

Fixes: #2625

state->signal_source,
*pkCFRunLoopDefaultMode);

state->loop = NULL;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we were not resetting this before?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It also looks like a race condition to me since it's modified without holding the mutex. uv__cf_loop_signal() reads it and runs on the other thread.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We weren't—in fact, we should never use or read this field again after this point in the code (we also never branch on it being NULL). The mistake was that we sometimes would (because we didn't hold the necessary mutex). Clearing it here just helps to makes any bad concurrent usage more obvious.

Copy link
Copy Markdown
Member

@saghul saghul left a comment

Choose a reason for hiding this comment

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

LGTM with a question

Copy link
Copy Markdown
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

LGTM but can I suggest splitting this into two commits so it's clear from history that state->loop = NULL is not part of the bug fix?

vtjnash added a commit to vtjnash/libuv that referenced this pull request Jan 16, 2020
The mutex also needs to protect the access to the state->loop variable,
since that's owned by the child thread and will be destroyed as soon as
it processes our message.

Fixes: libuv#2625
PR-URL: libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
vtjnash added a commit to vtjnash/libuv that referenced this pull request Jan 16, 2020
Set this to NULL just before disposing it to make mistakes more
painfully obvious, hopefully.

PR-URL: libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
@vtjnash
Copy link
Copy Markdown
Member Author

vtjnash commented Jan 16, 2020

The mutex also needs to protect the access to the state->loop variable,
since that's owned by the child thread and will be destroyed as soon as
it processes our message.

Fixes: libuv#2625
PR-URL: libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Set this to NULL just before disposing it to make mistakes more
painfully obvious, hopefully.

PR-URL: libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
@vtjnash vtjnash merged commit 07ddcb3 into libuv:v1.x Jan 21, 2020
liujinye-sys pushed a commit to open-vela/apps_system_libuv that referenced this pull request Jul 23, 2025
The mutex also needs to protect the access to the state->loop variable,
since that's owned by the child thread and will be destroyed as soon as
it processes our message.

Fixes: libuv/libuv#2625
PR-URL: libuv/libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
liujinye-sys pushed a commit to open-vela/apps_system_libuv that referenced this pull request Jul 23, 2025
Set this to NULL just before disposing it to make mistakes more
painfully obvious, hopefully.

PR-URL: libuv/libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
liujinye-sys pushed a commit to open-vela/apps_system_libuv that referenced this pull request Dec 16, 2025
The mutex also needs to protect the access to the state->loop variable,
since that's owned by the child thread and will be destroyed as soon as
it processes our message.

Fixes: libuv/libuv#2625
PR-URL: libuv/libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
liujinye-sys pushed a commit to open-vela/apps_system_libuv that referenced this pull request Dec 16, 2025
Set this to NULL just before disposing it to make mistakes more
painfully obvious, hopefully.

PR-URL: libuv/libuv#2626
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs_event_error_reporting segfault

3 participants