Skip to content

Commit ebacdee

Browse files
committed
Update how unstarted event loop v threads are created (Fixes #49)
1 parent bce3da0 commit ebacdee

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

core/src/main/java/io/netty/loom/EventLoopScheduler.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,16 @@ private static ThreadFactory newEventLoopSchedulerFactory(SharedRef sharedRef) {
9393
// and share it between the thread attachment and the SchedulingContext,
9494
// enabling
9595
// work-stealing to change it for both
96-
return runnable -> NettyScheduler.newEventLoopScheduledThread(() -> ScopedValue
97-
.where(CURRENT_SCHEDULER, new SchedulingContext(Thread.currentThread().threadId(), sharedRef))
98-
.run(runnable), sharedRef);
96+
var unstartedBuilder = Thread.ofVirtual();
97+
// we're not enforcing a preferred carrier on purpose, despite we could - to prevent leaks to happen:
98+
// once this scheduler is gone, but a virtual thread is still to complete, we would like to offload it
99+
// to the default scheduler rather than trying to reuse this scheduler's carrier thread
100+
return runnable -> unstartedBuilder.unstarted(() -> runWithContext(runnable, sharedRef), null, sharedRef);
101+
}
102+
103+
private static void runWithContext(Runnable runnable, SharedRef sharedRef) {
104+
ScopedValue.where(CURRENT_SCHEDULER, new SchedulingContext(Thread.currentThread().threadId(), sharedRef))
105+
.run(runnable);
99106
}
100107

101108
int externalContinuationsCount() {

core/src/main/java/io/netty/loom/NettyScheduler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,6 @@ public void onContinue(Thread.VirtualThreadTask virtualThreadTask) {
117117
jdkBuildinScheduler.onContinue(virtualThreadTask);
118118
}
119119

120-
static Thread newEventLoopScheduledThread(Runnable task, SharedRef sharedRef) {
121-
return Thread.VirtualThreadScheduler.newThread(task, sharedRef);
122-
}
123-
124120
public static boolean perCarrierPollers() {
125121
return ensureInstalled().perCarrierPollers;
126122
}

0 commit comments

Comments
 (0)