Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Understanding of task cancellation #15

Closed
prochri opened this issue Apr 27, 2024 · 8 comments
Closed

[Question] Understanding of task cancellation #15

prochri opened this issue Apr 27, 2024 · 8 comments

Comments

@prochri
Copy link

prochri commented Apr 27, 2024

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 added a commit that referenced this issue Apr 29, 2024
Previously the loop would wait for the next yield but instead we can
just step forward immediately when cancelling.

See #15
@rcarriga
Copy link
Contributor

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

@nickspacek
Copy link

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
Copy link

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 added a commit that referenced this issue May 1, 2024
@rcarriga
Copy link
Contributor

rcarriga commented May 1, 2024

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
Copy link

sovetnik commented May 2, 2024

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 added a commit that referenced this issue May 2, 2024
If a callback returns after a task is already cancelled, we should
gracefully handle it by not attempting to set the result of the task
since it is already set.

See #15
@rcarriga
Copy link
Contributor

rcarriga commented May 2, 2024

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

@DanilaMihailov
Copy link

fixed problem with "Future already set" for me nvim-neotest/neotest#402

@sovetnik
Copy link

sovetnik commented May 3, 2024

Looks like fixed! Thanks!

@rcarriga rcarriga closed this as completed Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants