[New-bugs-announce] [issue32181] runaway Tasks with Task.cancel() ignored.

Oleg K report at bugs.python.org
Thu Nov 30 08:29:19 EST 2017


New submission from Oleg K <windspirit at gmail.com>:

(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.

----------
components: asyncio
files: python_task_cancel.py
messages: 307307
nosy: Oleg K2, yselivanov
priority: normal
severity: normal
status: open
title: runaway Tasks with Task.cancel() ignored.
type: behavior
versions: Python 3.6
Added file: https://bugs.python.org/file47307/python_task_cancel.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32181>
_______________________________________


More information about the New-bugs-announce mailing list