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

Make DocHande !Sync #57

Closed
wants to merge 1 commit into from

Conversation

teohhanhui
Copy link
Contributor

Enforce at the type level that DocHandle is not to be shared across threads.

/cc @gterzian: bowtieworks/automerge_orm#3 (comment)

@@ -30,6 +32,7 @@ pub struct DocHandle {
/// that doesn't require a mutabale reference to the handle.
/// Note: the mutex is not shared between clones of the same handle.
last_heads: Mutex<Vec<ChangeHash>>,
_not_sync: PhantomData<Cell<&'static ()>>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

async fn run_bakery_algorithm(doc_handle: &DocHandle, customer_id: &String) {
let (our_number, closing) = doc_handle.with_doc_mut(|doc| {
async fn run_bakery_algorithm(doc_handle: &Mutex<DocHandle>, customer_id: &String) {
let (our_number, closing) = doc_handle.lock().await.with_doc_mut(|doc| {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the locking behaviour here and elsewhere is probably wrong, but I'm not sure what to do...

@gterzian if you could advise?

Enforce at the type level that DocHandle is not to be shared across
threads.
@gterzian
Copy link
Collaborator

gterzian commented Nov 15, 2023

@teohhanhui Thanks for the PR!

couple of questions after a quick look:

  1. Can we not just do impl !Sync for DocHandle?
  2. Why the mutex around the handle in the tests?

I'm guessing the mutex is necessary because tokio wants tasks to by sync? In that case it means we unfortunately cannot specify DocHandle to be !Sync, and it has to remain more of a convention that one shouldn't put it inside an Arc...

@teohhanhui
Copy link
Contributor Author

Can we not just do impl !Sync for DocHandle?

negative_impls is still unstable: rust-lang/rust#68318

The mutex is necessary because of tokio-rs/axum#1463

@gterzian
Copy link
Collaborator

The mutex is necessary because of tokio-rs/axum#1463

Ok I see, tokio only requires futures to be Send.

I think for now I would rather keep the doc handle as it is, and have a kind of loose convention that if relying on changed and sharing it with an arc could be confusing.

A good example would be https://docs.rs/tokio/latest/tokio/sync/watch/struct.Receiver.html, which also is Sync, even though sharing it across task is probably not a good idea.

@teohhanhui
Copy link
Contributor Author

teohhanhui commented Nov 16, 2023

A good example would be https://docs.rs/tokio/latest/tokio/sync/watch/struct.Receiver.html, which also is Sync, even though sharing it across task is probably not a good idea.

Actually it's documented to be fine: https://docs.rs/tokio/latest/tokio/sync/watch/index.html#thread-safety

Clones of Receiver handles may be moved to separate threads and also used concurrently.

Uhh, I see... That's fair, i.e. using it concurrently is technically correct but what you probably want to do is to clone it.

@gterzian
Copy link
Collaborator

using it concurrently is technically correct but what you probably want to do is to clone it.

Exactly

@teohhanhui teohhanhui closed this Nov 28, 2023
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.

2 participants