Comments (11)
Right, but the ConcurrentLinkedQueue
will keep growing more and more nodes, pointing to GCed bags.
from cats-effect.
It seems the leak is due to the nodes of the BagReferences
ConcurrentLinkedQueue
in FiberMonitor
, and the (empty) weakref objects in them. As far as I can see, these are never cleaned up, so the same would happen with normal threads too (except those are probably not created in an unbounded number).
from cats-effect.
Ideally we'd be using the carrier thread's local storage, not the virtual thread's for the bag. But that's probably not possible.
(I'm not seriously suggesting we use that, just found it interesting.)
from cats-effect.
Thanks, makes sense that we are not the only ones needing that 😇
I'm not seriously suggesting we use that
even if we wanted to these new JVMs make it annoying/impossible to access their internals
from cats-effect.
It seems the leak is due to the nodes of the
BagReferences
ConcurrentLinkedQueue
inFiberMonitor
, and the (empty) weakref objects in them.
@djspiewak as Daniel U pointed out, the leak is actually our fault:
We are lacking a "packing" mechanism for BagReferences
.
from cats-effect.
Hmm, that's actually really annoying. I think we'll want to rethink that thread-local strategy for virtual threads since there's a 1-to-1 fiber-to-virtual-thread mapping. Ideally we'd be using the carrier thread's local storage, not the virtual thread's for the bag. But that's probably not possible.
so the same would happen with normal threads too (except those are probably not created in an unbounded number).
But we should probably still fix this anyway.
from cats-effect.
You know, I'm actually very surprised that thread locals aren't just cleaned up with the thread itself. Arman is right that we really want the carrier thread here, not the virtual thread, but even with the virtual thread this doesn't feel like a thing that should be leaking (just a thing that would be very inefficient).
from cats-effect.
Right but that's a weak reference, so it shouldn't prevent the cleanup of bag
.
from cats-effect.
The solution is probably to do something annoying with ReferenceQueue
and periodically check that queue for size. Once the queue gets big enough, we go through and purge out the collected refs in Bags
. This would have to be amortized into monitorSuspended
or similar, creating a moderate overhead. Probably not enough overhead to worry about gating it to virtual threads, so this would also work to resolve the similar issue with blocking
.
We wouldn't need JDK 21 for this (since it's not loom specific), so we could probably resolve it in 3.5.x
from cats-effect.
I've proposed a fix for the general issue in #3964.
But from a performance standpoint I still think that we need a different solution for virtual threads. The current mechanism adds a lot of overhead:
- a single point of contention for registering every thread
- allocating an entire
WeakBag
(intended to be long-lived and hold references to many fibers over its lifetime) for a virtual thread, even though we will only place a single fiber in there - nested layers of weak references, which we know that the GC just loves /s
Specifically we hold a weak reference to a bag which itself hold a weak reference to the fiber.
from cats-effect.
Fixed the leak in #3964 and opened a follow-up.
from cats-effect.
Related Issues (20)
- `CallbackStack#pack` can double-count removals when called concurrently HOT 5
- dispatcher document warning about bounded queue
- sequential dispatcher: race condition in release `step` causes disorder
- Delaying a task to be run far into the future can prevent concurrently scheduled tasks from running HOT 3
- Document stack-safety requirements of typeclasses
- `Dispatcher#unsafeRunTimed` should also `cancel()` if `Await.result` is `interrupt`ed HOT 7
- Flakiness in `SchedulerSpec` HOT 1
- Flakiness in `SupervisorSpec` HOT 1
- Dispatcher sequential runs queued tasks concurrently when closing HOT 2
- Flakiness in `IOSpec` HOT 1
- better handling of callbacks that might throw in `CallbackStack` HOT 1
- Allow overriding how fatal errors are printed HOT 7
- Improve `MonadCancel` scaladoc HOT 4
- Cancelling `Async` queue `take` makes other `take` hang HOT 5
- `IO#asyncCheckAttempt` is inconsistent with `Async#asyncCheckAttempt`
- Improve contributor documentation HOT 3
- Add (best-effort) stealing API to polling system
- Add API to polling system to attempt to get current poller without shifting HOT 6
- More efficient monitoring of fibers on virtual threads HOT 1
- Published tutorial older than tutorial.md HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats-effect.