Skip to content

Fix race condition on wait() #501

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

Closed
wants to merge 1 commit into from
Closed

Fix race condition on wait() #501

wants to merge 1 commit into from

Conversation

foriequal0
Copy link
Contributor

@foriequal0 foriequal0 commented Oct 14, 2019

wait() using oneshot::channel() makes race conditions. Call Executor::wait() directly.

It is possible that the thread that has executed wait(), which is usually the main thread, terminates before the thread executing done_tx.send(()) drops rpc_handler.
Static variables are destructed at the end of termination of the main
thread, and then a segfault occurs when the thread dropping the
rpc_handler accesses the variables.

@parity-cla-bot
Copy link

It looks like @foriequal0 hasn't signed our Contributor License Agreement, yet.

The purpose of a CLA is to ensure that the guardian of a project's outputs has the necessary ownership or grants of rights over all contributions to allow them to distribute under the chosen licence.
Wikipedia

You can read and sign our full Contributor License Agreement at the following URL: https://cla.parity.io

Once you've signed, please reply to this thread with [clabot:check] to prove it.

Many thanks,

Parity Technologies CLA Bot

`wait()` using `oneshot::channel()` makes race conditions. Call
`Executor::wait()` directly.

It is possible that the thread that has executed `wait()`, which is
usually the main thread, terminates before the thread executing
`done_tx.send(())` drops `rpc_handler`.
Static variables are destructed at the end of termination of the main
thread, and then a segfault occurs when the thread dropping the
`rpc_handler` accesses the variables.
@foriequal0
Copy link
Contributor Author

[clabot:check]

@parity-cla-bot
Copy link

It looks like @foriequal0 signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

At first glance this looks excellent, thank you for the contribution!

Copy link
Contributor

@tomusdrw tomusdrw left a comment

Choose a reason for hiding this comment

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

@foriequal0 thank you for the contribution, the code looks super clean now.

I think we have changed the behavior slightly though - if you want to wait for the server to finish - we only wait for the executor to finish.

I see one issue with the current implementation though: If the event loop is external wait is pretty much a no-op, so we definitely won't wait for the server to finish.
Any ideas how to address that?

@foriequal0
Copy link
Contributor Author

foriequal0 commented Oct 15, 2019

@tomusdrw I just realized that I can fix this issue with a few lines of changes while not changing the behavior.
Future::select pass the unresolved future, which is server holding rpc_handler, to the following chain. Therefore, it is dropped after the done_tx.send(()). I didn't know that this is the reason at that time.
Dropping them before send is enough to fix this issue, and it doesn't change the behavior.

  1. Here: https://github.com/paritytech/jsonrpc/blob/master/http/src/lib.rs#L568
  2. and here: https://github.com/paritytech/jsonrpc/blob/master/ipc/src/server.rs#L243

Should I revert the commit and proceed with this idea?

@foriequal0
Copy link
Contributor Author

I'll close this and create a new PR(#504) with the suggested fix

@foriequal0 foriequal0 closed this Oct 16, 2019
@foriequal0 foriequal0 deleted the fix/wait-race branch October 16, 2019 07:46
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.

4 participants