Skip to content

Avoid invoking writeFunction under lock in ChannelSendOperator - #36361

Merged
bclozel merged 2 commits into
spring-projects:7.0.xfrom
l2yujw:channel-send-operator-lock-scope
Oct 21, 2026
Merged

bclozel merged 2 commits into
spring-projects:7.0.xfrom
l2yujw:channel-send-operator-lock-scope

Conversation

@l2yujw

@l2yujw l2yujw commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

ChannelSendOperator transitions to FIRST_SIGNAL_RECEIVED on the first signal and invokes the provided writeFunction to start the actual write pipeline.

Previously, writer invocation could occur while holding the operator's monitor lock, which increases lock hold time and can block cross-thread access or lead to re-entrant deadlock scenarios depending on the writer implementation.

This change limits synchronization to state transitions and signal bookkeeping and moves writer invocation and subscription outside the synchronized block.

A regression test verifies that the writer function is not invoked while holding the monitor lock by asserting another thread can acquire the publisher's monitor during invocation.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 20, 2026

@rstoyanchev rstoyanchev left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the suggestion. To make the review (and history) easier, could you eliminate stylistic differences, i.e. not switching from if-else to if with return?

@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 20, 2026
@l2yujw
l2yujw force-pushed the channel-send-operator-lock-scope branch from 1b30814 to ca2c2c8 Compare February 20, 2026 14:22
@l2yujw

l2yujw commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion — agreed. I updated the change to keep the original if/else structure. The diff is now limited to moving writeFunction invocation (and subscription) outside the synchronized block, without additional stylistic restructuring.

@l2yujw
l2yujw requested a review from rstoyanchev February 24, 2026 15:37
@l2yujw

l2yujw commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping — changes from your feedback are already applied. Let me know if anything else is needed!

@l2yujw

l2yujw commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

Hi @rstoyanchev, just following up on this PR. The requested stylistic cleanup has been applied, and the branch is now up to date with the current main and remains conflict-free. The original locking issue is also still present on main.

Would you be open to taking another look, or is there anything else you’d like me to adjust?

@bclozel
bclozel changed the base branch from main to 7.0.x October 21, 2026 08:14
@bclozel bclozel self-assigned this Oct 21, 2026
@bclozel bclozel added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 21, 2026
@bclozel bclozel added this to the 7.0.11 milestone Oct 21, 2026
l2yujw and others added 2 commits October 21, 2026 10:39
Prior to this commit, ChannelSendOperator invoked the write function
and subscribed to its result while holding the subscriber monitor.
A write function that re-enters the operator, or that blocks waiting
on another thread signalling the same subscriber, could deadlock;
this was flagged by a long-standing FIXME.

This commit records the first-signal state inside the synchronized
block, then applies the write function after the lock is released.
Signals arriving in between are buffered as before, as the state is
already FIRST_SIGNAL_RECEIVED. The change applies to both the web and
messaging variants of the operator.

Signed-off-by: l2yuPa <jeungwon28@gmail.com>
@bclozel
bclozel force-pushed the channel-send-operator-lock-scope branch from 1581ec9 to 40664f7 Compare October 21, 2026 08:39
@bclozel
bclozel merged commit 40664f7 into spring-projects:7.0.x Oct 21, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants