S6906: Virtual threads should not run tasks that include synchronized or native code

Description

The CPU usage optimization introduced with VirtualThread relies on the fact that these new types of threads can be “mounted” and “dismounted” from an OS thread whenever they find themselves waiting for some blocking operation ( I/O, network, etc..). When the task wrapped by the virtual thread runs some synchronized or native code, it will get pinned to its current underlying OS thread. If during this time a blocking operation occurs, the virtual thread will not be dismounted, blocking the OS thread, and defeating the purpose of using a virtual thread in the first place.

This new rule should try to detect all cases where a task used to construct a new virtual thread, or passed to a Executors.newVirtualThreadPerTaskExecutor() includes synchronized code or native methods, and raise an issue.

It could be better implemented in the architecture-plugin probably.

Note: The rule could be tricky to implement without too many FPs, as we could make wrong assumptions about the intentions behind the use of a synchronized block. In general, we will suggest avoiding VirtualThreads executing synchronized code with blocking operations within, as it defeats their purpose.

Activity

Jonathan Vila LopezMarch 13, 2024 at 3:55 PM

are we finally going to implement this rule ? I mean by the note included in this description is not clear to me.

Done
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Components

Fix versions

Priority

Sentry

Created February 7, 2024 at 2:21 PM
Updated October 16, 2024 at 2:06 PM
Resolved March 20, 2024 at 10:30 AM