Skip to content

Fix unsafe modification of Trampoline resources#2031

Merged
alexandru merged 22 commits into
monix:mainfrom
AVSystem:safe-reference-publishing-to-upstream
May 4, 2026
Merged

Fix unsafe modification of Trampoline resources#2031
alexandru merged 22 commits into
monix:mainfrom
AVSystem:safe-reference-publishing-to-upstream

Conversation

@halotukozak

Copy link
Copy Markdown
Contributor

Original contribution from the AVSystem's fork made by @mikkolaj
AVSystem#3

Forked executions of Trampoline tasks operated on the same data structures as the thread-local Trampoline. Consequently, multiple threads were able to concurrently access same non-thread-safe resources.

Fixed by operating solely on copied data in the forked Trampoline instead of referencing the previous-thread-local Trampoline.

Copilot AI review requested due to automatic review settings April 10, 2026 08:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a concurrency bug in Monix’s Trampoline execution machinery where forked/resumed executions could reference and mutate the same non-thread-safe queue across threads, risking concurrent modification and dropped/stuck tasks.

Changes:

  • Refactors Trampoline to resume forked execution on an appropriate trampoline (thread-local on JVM) and to avoid sharing mutable queue state across threads.
  • Updates TrampolineExecutionContext (JVM + JS) to use the new trampoline backing abstraction and adds a JVM regression test for nested executions under various schedulers/blocking.
  • Cleans up ChunkedArrayQueue APIs/tests and updates/adds benchmarks related to trampolining and chunked processing.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
monix-execution/shared/src/test/scala/monix/execution/internal/collection/ChunkedArrayQueueSuite.scala Removes test for removed enqueueAll(Iterable) overload; minor import formatting.
monix-execution/shared/src/main/scala/monix/execution/internal/Trampoline.scala Core fix: resume-on-correct-trampoline behavior and EC modeling to avoid unsafe shared queue mutation.
monix-execution/shared/src/main/scala/monix/execution/internal/collection/ChunkedArrayQueue.scala Removes enqueueAll(Iterable) overload and small iterator visibility tweaks.
monix-execution/jvm/src/test/scala/monix/execution/schedulers/TrampolineExecutionContextSuite.scala Adds JVM regression coverage for nested trampolined executions, blocking, and scheduler variants.
monix-execution/jvm/src/main/scala/monix/execution/schedulers/TrampolineExecutionContext.scala Adapts JVM trampoline EC to new TrampolineEC and thread-local resume trampoline behavior.
monix-execution/js/src/main/scala/monix/execution/schedulers/TrampolineExecutionContext.scala Adapts JS trampoline EC to new TrampolineEC model.
benchmarks/shared/src/main/scala/monix/benchmarks/TrampolineExecutionContextBenchmark.scala Adds new benchmark targeting trampoline + blocking behavior.
benchmarks/shared/src/main/scala/monix/benchmarks/TrampolinedRunnableBenchmark.scala Fixes benchmark instructions and re-enables deep benchmark with corrected loop.
benchmarks/shared/src/main/scala/monix/benchmarks/ChunkedMapFilterSumBenchmark.scala Minor benchmark adjustments (imports + explicit chunk typing).
benchmarks/shared/src/main/scala/monix/benchmarks/ChunkedEvalFilterMapSumBenchmark.scala Minor benchmark adjustments (imports + explicit chunk typing).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@alexandru

Copy link
Copy Markdown
Member

FYI, this PR is next on my radar.

I'm doing other changes, and it may be cool if we have a way for me to suggest changes, primarily “merge with main” :))

Unsure if you can check the allow edits from maintainers option (although this may mean a security risk for you), otherwise I'll have to either duplicate the PR or make a PR to your original branch.

Or we can just sync via comments, but this has a high latency, so whatever works for you, I'm fine with it.

@halotukozak

Copy link
Copy Markdown
Contributor Author

FYI, this PR is next on my radar.

I'm doing other changes, and it may be cool if we have a way for me to suggest changes, primarily “merge with main” :))

Unsure if you can check the allow edits from maintainers option (although this may mean a security risk for you), otherwise I'll have to either duplicate the PR or make a PR to your original branch.

Or we can just sync via comments, but this has a high latency, so whatever works for you, I'm fine with it.

@ddworak can you check these options?
@mikkolaj fyi

@halotukozak

Copy link
Copy Markdown
Contributor Author

FYI, this PR is next on my radar.
I'm doing other changes, and it may be cool if we have a way for me to suggest changes, primarily “merge with main” :))
Unsure if you can check the allow edits from maintainers option (although this may mean a security risk for you), otherwise I'll have to either duplicate the PR or make a PR to your original branch.
Or we can just sync via comments, but this has a high latency, so whatever works for you, I'm fine with it.

@ddworak can you check these options? @mikkolaj fyi

GitHub currently does not support the "Allow edits from maintainers" feature for organization-owned forks.
This toggle is strictly reserved for forks owned by individual user accounts.
I've updated the branches to the latest main.

@ddworak

ddworak commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Apparently this only works for user-owned forks, but I've added @alexandru as external write-level collaborator to this repository. Great to see you back!

@alexandru alexandru merged commit 5ad13b0 into monix:main May 4, 2026
11 checks passed
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