nvim-neotest / nvim-nio

A library for asynchronous IO in Neovim
MIT License
288 stars 8 forks source link

[Question] Understanding of task cancellation #15

Closed prochri closed 3 months ago

prochri commented 5 months ago

I am trying to close a (potentially long running) process, if it's task is cancelled for some reason (e.g. timeout). Here is a minimal example without processes:

nio.run(function()
  local p_handle = nio.run(function()
    nio.sleep(5000)
  end, function()
    print("canceled or success")
    -- do cleanup e.g. send a signal to a nio process
  end)
  nio.sleep(1000)
  print("cancel the handle!")
  p_handle.cancel()
end)

My expectation would be, that after 1 second, both 'cancel the handle!' and 'canceled or success' (caused by the task cancellation) would be printed. However, the callback function is only called after the full 5 seconds.

Is this the intended behavior? If yes, what would be the proper way to do this?

rcarriga commented 5 months ago

Ah no that is not intended behaviour :sweat_smile: I've pushed a fix and it should be working as you'd expect now

nickspacek commented 5 months ago

Hmm, started seeing an exception Future already set thrown from neotest that seems related.

https://github.com/nvim-neotest/neotest/blob/master/lua/neotest/client/strategies/integrated/init.lua#L47

sovetnik commented 5 months ago

Agree, I'm facing it too

Error executing vim.schedule lua callback: ...nvim/site/pack/packer/start/nvim-nio/lua/nio/control.lua:133: Future already set
stack traceback:
        [C]: in function 'error'
        ...nvim/site/pack/packer/start/nvim-nio/lua/nio/control.lua:133: in function 'set_error'
        ...e/nvim/site/pack/packer/start/nvim-nio/lua/nio/tasks.lua:93: in function 'close_task'
        ...e/nvim/site/pack/packer/start/nvim-nio/lua/nio/tasks.lua:111: in function 'cb'
        ...e/nvim/site/pack/packer/start/nvim-nio/lua/nio/tasks.lua:185: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
Press ENTER or type command to continue
rcarriga commented 5 months ago

I'm not seeing the same issue myself but I've pushed a change that I think should fix it. If not, could someone provide a simple reproduction if possible?

sovetnik commented 5 months ago

I have just checked, bug is still here. To reproduce open any test (in my case it is an Elixir test) , start watcher and after test finishes and marks are set this error message appears.

rcarriga commented 5 months ago

Ah yes I was able to reproduce with that thank you. I've pushed another fix which should now address the issue

DanilaMihailov commented 5 months ago

fixed problem with "Future already set" for me https://github.com/nvim-neotest/neotest/issues/402

sovetnik commented 4 months ago

Looks like fixed! Thanks!