This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: runaway Tasks with Task.cancel() ignored.
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Oleg K2, xtreak, yselivanov
Priority: normal Keywords:

Created on 2017-11-30 13:29 by Oleg K2, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python_task_cancel.py Oleg K2, 2017-11-30 13:29
Messages (2)
msg307307 - (view) Author: Oleg K (Oleg K2) Date: 2017-11-30 13:29
(tested in VM and in real linux)

there is an issue with Task, in some cases task will ignore cancellation and will keep running for a while.

there is some explanation needed regarding the python_task_cancel.py example.

1)there is a async Integer Generator (my_generator) it has sleep() inside, and produces integers.

2)there is  async coro test(), which is consumer of integers , it has several
	while True:
		consume = yield

3) there are tasks "a_task()", the job task is doing: it reads from generator, and pushes integer to active "async coro test()"

	my_generator -iterated by->  a_task() -sends Int to-> test()

	
4) "async coro test()" is protected by lock, so only one task may push value to it at a time. 

5) "task_context" is a context manager, a helper which starts tasks and cancels them 
	
	__aenter__ -> loop.create_task(self.task)
	__aexit__ -> self.running_task.cancel()
		

		
    here is how is it used
	
	async with task_context( async-coro-to-be-started-as-a-task ):
	# task is created 
		while True:
			consume = yield
			if consume > 10 :break
	# task is CANCELLED 	 
			

------------------------------------------------------


The Case:			


    the issue is there when there is sequential aenters and aexits 
	
	
	async with task_context( async-coro-to-be-started-as-a-task ):
	# task #1 is created 
		while True:
			consume = yield
			if consume > 10 :break
	# task #1 is CANCELLED 	 

    
    async with task_context( async-coro-to-be-started-as-a-task ):
	# task #2 is created 
		while True:
			consume = yield
			if consume > 20 :break
	# task #2 is CANCELLED 	 

	
	async with task_context( async-coro-to-be-started-as-a-task ):
	# task #3 is created 
		while True:
			consume = yield
			if consume > 10 :break
	# task #3 is CANCELLED 	 

	

what may go wrong here? - at the end there will be 3 live task reading from same generator!
2 tasks have clearly got cancel() call but, according to output they still there and working:

there is part of output as a proof:

my_generator: [0.3] PRODUCE VALUE 28
send done: 20 task id= 1
will send : 28 task id= 4
consume 3  28
my_generator: [0.3] PRODUCE VALUE 29
send done: 28 task id= 4
will send : 29 task id= 2
consume 3  29
send done: 29 task id= 2
my_generator: [0.4] PRODUCE VALUE 21
will send : 21 task id= 1
consume 3  21
send done: 21 task id= 1
my_generator: [0.3] PRODUCE VALUE 30
will send : 30 task id= 3
consume 3  30
send done: 30 task id= 3
my_generator: [0.3] PRODUCE VALUE 31
will send : 31 task id= 3
	

"task id= 3"
"task id= 2"
"task id= 4" 

that means that all tasks are there!, which should not happen. 
but, these tasks WILL GET cancellation call execute just after "async coro test()" will exit, that exit somehow  will trigger pending exit of tasks, which should have happened long ago!.


also, there is no reliable way to wait task termination, which is also major issue with real life asyncio usage,
i need to have a mean to wait for task to complete wait until its "finally:" is done and all resources are free.
msg318072 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 19:23
I think this is caused by https://bugs.python.org/issue30773 (a bug in asynchronous generators)
History
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76362
2022-03-12 15:49:28asvetlovsetstatus: open -> closed
resolution: duplicate
stage: resolved
2018-09-22 16:47:41xtreaksetnosy: + xtreak
2018-05-29 19:23:52yselivanovsetmessages: + msg318072
2017-11-30 13:29:19Oleg K2create