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

Dispatcher sequential runs queued tasks concurrently when closing #3981

Closed
mperucca opened this issue Jan 30, 2024 · 2 comments
Closed

Dispatcher sequential runs queued tasks concurrently when closing #3981

mperucca opened this issue Jan 30, 2024 · 2 comments

Comments

@mperucca
Copy link

mperucca commented Jan 30, 2024

This might be specific to unsafeToFuture, but closing a Dispatcher runs all queued tasks and does so concurrently, which was even more alarming for sequential, as seen below.

val (dispatcher, cancel) = Dispatcher.sequential[IO].allocated.unsafeRunSync()
val buffer = ArrayBuffer[Int]()
for (i <- 0 to 99) {
  dispatcher.unsafeToFuture(IO(buffer += i))
}
cancel.unsafeRunSync() // buffer stays ordered if this line is removed (or the dispatcher finished already)

A test case in my code that initially hinted at this unexpected behavior was the following:

val (dispatcher, cancel) = Dispatcher.sequential[IO].allocated.unsafeRunSync()
dispatcher.unsafeToFuture(IO.never)
dispatcher.unsafeToFuture(IO.println("this runs when the dispatcher is closed"))
cancel.unsafeToFuture()

3.4.8 is the last version that behaves as expected.

This seems similar to #3945, but the problem seemed to go farther back, possibly to #3510.

@armanbilge
Copy link
Member

Thanks for reporting! Note that we've got a significant rewrite of Dispatcher in flight which should hopefully fix this and many other bugs (but I have not verified this yet).

@djspiewak
Copy link
Member

Wow this is amazing. I don't have any intuition about how this is happening, and I kinda don't want to. :P The rewrite definitely doesn't have this issue.

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

No branches or pull requests

3 participants