You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you cancel parent context, all childs cancels without particular order. (Parent context could exit erlier than his child and its child could get unpredicted execution behaviour when try to use some parent resources which is already disposed)
There is example.
It is recursive function which builds context tree like this: 0 (background)->1->2->3->4->5->6->7->8->9->10
and tries to cancel it from root object 0(backhround).
go run main.go
child object 1 with parent 0 started
child object 2 with parent 1 started
child object 3 with parent 2 started
child object 4 with parent 3 started
child object 5 with parent 4 started
child object 6 with parent 5 started
child object 7 with parent 6 started
child object 8 with parent 7 started
child object 9 with parent 8 started
child object 10 with parent 9 started
cancel main context
object 7 canceled
object 3 canceled
object 5 canceled
object 1 canceled
object 6 canceled
object 9 canceled
object 4 canceled
object 2 canceled
object 8 canceled
object 10 canceled
finishing
As you can see cancellation is without particular order inspite of that fact what context mechanism should wait till all subchilds call cancel() function to avoid context leaks.
Expected beheviour:
child object 1 with parent 0 started
child object 2 with parent 1 started
child object 3 with parent 2 started
child object 4 with parent 3 started
child object 5 with parent 4 started
child object 6 with parent 5 started
child object 7 with parent 6 started
child object 8 with parent 7 started
child object 9 with parent 8 started
child object 10 with parent 9 started
cancel main context
object 10 canceled
object 9 canceled
object 8 canceled
object 7 canceled
object 6 canceled
object 5 canceled
object 4 canceled
object 3 canceled
object 2 canceled
object 1 canceled
finishing
The text was updated successfully, but these errors were encountered:
The cancellation does follow the tree downwards in order, however, what you're experiencing is that the scheduler makes no guarantee that any goroutine will run immediately when available, so even though the cancellation is in the correct order, any code attached to it might not be.
You will need some other synchronization if you depend on shared resources.
When you cancel parent context, all childs cancels without particular order. (Parent context could exit erlier than his child and its child could get unpredicted execution behaviour when try to use some parent resources which is already disposed)
There is example.
It is recursive function which builds context tree like this: 0 (background)->1->2->3->4->5->6->7->8->9->10
and tries to cancel it from root object 0(backhround).
Output is:
As you can see cancellation is without particular order inspite of that fact what context mechanism should wait till all subchilds call cancel() function to avoid context leaks.
Expected beheviour:
The text was updated successfully, but these errors were encountered: