Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/common/src/JobSupport.kt (Kotlin)

Code before:
                                     * whether the ChildCompletion was added to the list or not. If not -- the JobNode.invoke will do that.
                                     * See comment to JobNode.invoke, we cannot differentiate the situation when external state updater
                                     * invoked or skipped the node, thus we additionally synchronize on 'markInvoked'.
                                     */
                                    if (node is ChildCompletion && !node.markInvoked()) return node
                                    // The state can only change to Complete here, so the node can stay in the list, just retry
                                    return@loopOnState
                                }
                                // just return node if we don't have to invoke handler (not cancelling yet)
                                if (rootCause == null) return node
                                // otherwise handler is invoked immediately out of the synchronized section & handle returned
                                handle = node
                            }
                        }
                    }
                    if (rootCause != null) {
                        // Note: attachChild uses invokeImmediately, so it gets invoked when adding to cancelled job
                        if (invokeImmediately) {
                            node.invoke(rootCause)
                        }
                        return handle
                    } else {
                        if (!nodeAdded) list.addLast(node)
                        if (this.state !== state) {
                            // Here again, we try to prevent concurrent finalization of the parent,
                            // if the parent fails to add ChildCompletion because the child changed it's state to Completed.
                            if (node is ChildCompletion && !node.markInvoked()) return node
                            // If the state has changed to Complete, the node can stay in the list, just retry.
                            if (this.state !is Incomplete) return@loopOnState
                            // If the state is Incomplete, set the flag that the node is already added to the list instead of removing it.
                            nodeAdded = true
                        } else {
                            return node
                        }
                    }
                }
                else -> { // is complete
                    if (invokeImmediately) {
                        node.invoke((state as? CompletedExceptionally)?.cause)
                    }
                    return NonDisposableHandle
                }
            }
        }
    }

    private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode {
        val node = if (onCancelling) {
            (handler as? JobCancellingNode)
                ?: InvokeOnCancelling(handler)
        } else {

Review comment (suggestion):
`addLastIf` is no longer used and only required in tests, you can safely remove it

Code after:
                                     * whether the ChildCompletion was added to the list or not. If not -- the JobNode.invoke will do that.
                                     * See comment to JobNode.invoke, we cannot differentiate the situation when external state updater
                                     * invoked or skipped the node, thus we additionally synchronize on 'markInvoked'.
                                     */
                                    if (node is ChildCompletion && !node.markInvoked()) return node
                                    // The state can only change to Complete here, so the node can stay in the list, just retry
                                    return@loopOnState
                                }
                                // just return node if we don't have to invoke handler (not cancelling yet)
                                if (rootCause == null) return node
                                // otherwise handler is invoked immediately out of the synchronized section & handle returned
                                handle = node
                            }
                        }
                    }
                    if (rootCause != null) {
                        // Note: attachChild uses invokeImmediately, so it gets invoked when adding to cancelled job
                        if (invokeImmediately) {
                            node.invoke(rootCause)
                        }
                        return handle
                    } else {
                        if (!isNodeAdded) list.addLast(node)
                        if (this.state !== state) {
                            // Here again, we try to prevent concurrent finalization of the parent,
                            // if the parent fails to add ChildCompletion because the child changed it's state to Completed.
                            if (node is ChildCompletion && !node.markInvoked()) return node
                            // If the state has changed to Complete, the node can stay in the list, just retry.
                            if (this.state !is Incomplete) return@loopOnState
                            // If the state is Incomplete, set the flag that the node is already added to the list instead of removing it.
                            isNodeAdded = true
                        } else {
                            return node
                        }
                    }
                }
                else -> { // is complete
                    if (invokeImmediately) {
                        node.invoke((state as? CompletedExceptionally)?.cause)
                    }
                    return NonDisposableHandle
                }
            }
        }
    }

    private fun makeNode(handler: CompletionHandler, onCancelling: Boolean): JobNode {
        val node = if (onCancelling) {
            (handler as? JobCancellingNode)
                ?: InvokeOnCancelling(handler)
        } else {