Cannot cancel thread

So have a serversided function in which multiple threads get created. I’m adding all the threads to a table and returning the table so I can cancel all the running threads. It’s working with the first 3, but when i try to cancel the 4rd one its erroring.


This is the Thread which causes the error.

image
This is the loop canceling all the threads.

image
This is the output error.

Can anyone help me, please?

Thanks for reading and sorry for my bad english…

Are you calling cancel on the same thread that is currently running? You can check this by running:

print(thread == coroutine.running())

This is the most likely cause of the error.

1 Like

So as you can see the task.cancel isnt inside the task.spawn so it shouldnt be that problem.

I found a solution by just checking if the crashed attribute is false in the while loop (“while task.wait(1) and map:GetAttribute(“Crashed”) == false do”) and its working, but I would be interested in the problem i showed and why its erroring…

It’s erroring because the thread is running, and you can’t stop a running thread

You can, this is exactly what this command is for. It’s working for other threads in this script too!

3 Likes

I’m running into some confusion so I’m reviving this thread.
What is the point of task.cancel() if not to cancel an actively running thread? The documentation is extremely spotty and unclear: task | Documentation - Roblox Creator Hub. What does it mean by “the currently executing thread… may not be canceled”? Is that not the whole point of task.cancel(), to take in some thread that you have actively and currently running, and then kill it?

If for whatever reason it is unable to do its one job as stated in its name “cancel”, is it necessary to task.wait(5,runningthread) first, followed by task.cancel(runningthread)
If it is necessary to pause a thread before cancelling it the documentation should be updated to reflect this as this can be a major point of confusion.

1 Like

so you say i simply have to add a wait?

The documentation is not clear about the conditions that make threads unable to be cancelled. My intuition says that if some particular thread is going to error when you cancel it, then pausing it and cancelling it still won’t work because the engine just absolutely will “not allow” that thread to be cancelled. I’m going to avoid using task.cancel altogether and just have the thread check some condition and break its own loop once the condition is true

I’m having this same problem. I read over the documentation MANY times and still am not understanding what they mean. From my experience, you can’t cancel the thread (coroutine) if it’s either “normal” or “dead”, and that’s what’s stated in the documentation, but what does “normal” mean? It’s not elaborated upon.

It seems that sometimes canceling threads work, and sometimes it doesn’t. I also apologize for the bump.

Actually yesterday i just thought about this post and a solution for it too…
I came to the result that coroutines might be the better choice for cancelling threads, I mean you can call coroutine.close(co) instead of task.cancel(f) and the documentation for this is more clear.

“A coroutine that is currently running cannot be closed.” - Documentation

Testing this statement

This part is actually pretty interesting… the documenation also talks about not beeing able to cancel some threads, for example when they’re running but i just did a quick test:

local co = coroutine.create(function()
	while task.wait() do
		print("running")
	end
end)

coroutine.resume(co)
task.wait(1)
coroutine.close(co)

The prints stop after 1 second of waiting, even if the documentation says it shouldn’t be possible, right?

As a final result I can defenetly recommend using coroutine.close instead of task.close all the time, as I can see no advantage in task.close. Roblox should depecate it.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.