luv: Unable to `cancel` a `luv_work_ctx_t` handle
RE: #629
If you create new work via the uv.new_work
function and then queue it with the queue_work
function, how do you go about “killing” said work?
Some example code to show what I mean
local luv = require("luv")
local work_fn = function()
local count = 0
while count > 0 do
count = count + 1
end
return
end
local after_work_fn = function()
print("Work Complete!")
end
local work = luv.new_work(work_fn, after_work_fn)
work:queue()
As we see in the above code, work_fn
will never “exit” and thus run infinitely. I don’t see anything in the docs about how to stop “rogue” work processes. Of course, one isn’t going to intentionally create code that will run forever, but it is certainly possible that code could end up doing so. For my use case, I am looking more from the angle of “code is running too long and user wants it to stop”. I don’t see how I would go about doing that.
Am I missing something?
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 1
- Comments: 16 (9 by maintainers)
Well that’s not the answer I was hoping for lol. I appreciate the direction. It appears that the request to get
cancel
implemented is still needed as it doesn’t exist in the luv implementation oflibuv
, however it also sounds like getting that implemented won’t address my specific issue. Should I close this thread out or does the luv team want this to stay open to track theluv_work_ctx_t
cancel work?It’s not all that uncommon, stopping an in-flight work request requires preempting, which is quite difficult to do (nearly impossible at Libuv’s level).
As far as I’m aware Libuv doesn’t provide any way to stop, kill, or otherwise halt a spawned thread; much less do the same for an individual work request (which will be running on one of Libuv’s threadpool threads).
If you cannot guarantee that the work will ever finish, you’ll either need to delegate it to its own thread (at which point the os scheduler will handle it behaving poorly) or instrument it with your own code that will preempt it if it runs for too long or something similar. For the latter, you can use
debug.sethook
.Little bit of diving into the libuv docs, I dug up this tidbit
However, when modifying the above code to try and use
uv.cancel
, I get the following errorNote, updated code
Digging deeper still, the cancel doc actually says
If I am reading that correctly, does that mean that you can’t cancel a
work_ctx_t
handle? Noting specifically that thenew_work
calls out that you do not get awork_t
handle.