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

Code before:
                                // check if we are installing cancellation handler on job that is being cancelled
                                rootCause = state.rootCause // != null if cancelling job
                                // We add node to the list in two cases --- either the job is not being cancelled
                                // or we are adding a child to a coroutine that is not completing yet
                                if (rootCause == null || handler.isHandlerOf<ChildHandleNode>() && !state.isCompleting) {
                                    // Note: add node the list while holding lock on state (make sure it cannot change)
                                    nodeAdded = node
                                    if (!addLast(state, list, node)) {
                                        // parent failed to add ChildCompletion node to the child, the child became Completed
                                        // if ChildCompletion is already invoked, child started parent completion
                                        // return handler, so that parent would wait for the child completion
                                        if (node is ChildCompletion && !node.markInvoked()) return node
                                        return@loopOnState // retry
                                    }
                                    // 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) {
                                if (nodeAdded != null) {
                                    nodeAdded!!.invoke(rootCause)
                                } else {
                                    handler.invokeIt(rootCause)
                                }
                            }
                            return handle
                        } else {
                            // if ChildCompletion is already invoked, return handler, so that parent would wait for the child to complete
                            nodeAdded = node
                            if (addLast(state, list, node) || node is ChildCompletion && !node.markInvoked()) {
                                return node
                            }
                        }
                    }
                }
                else -> { // is complete
                    // :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension,
                    // because we play type tricks on Kotlin/JS and handler is not necessarily a function there
                    if (invokeImmediately) {
                        if (nodeAdded != null) {
                            nodeAdded!!.invoke((state as? CompletedExceptionally)?.cause)
                        } else {
                            handler.invokeIt((state as? CompletedExceptionally)?.cause)
                        }
                    }
                    return NonDisposableHandle

Review comment (suggestion):
Seems like it can be replaced with `(nodeAdded ?: node).invoke(rootCause)`

Code after:
                    var rootCause: Throwable? = null
                    var handle: DisposableHandle = NonDisposableHandle
                    if (onCancelling && state is Finishing) {
                        synchronized(state) {
                            // check if we are installing cancellation handler on job that is being cancelled
                            rootCause = state.rootCause // != null if cancelling job
                            // We add node to the list in two cases --- either the job is not being cancelled
                            // or we are adding a child to a coroutine that is not completing yet
                            if (rootCause == null || handler.isHandlerOf<ChildHandleNode>() && !state.isCompleting) {
                                // Note: add node the list while holding lock on state (make sure it cannot change)
                                if (!isNodeAdded) list.addLast(node)
                                if (this.state !== state) {
                                    /*
                                     * Here we have an additional check for ChildCompletion. Invoking the handler once is not enough
                                     * for this particular kind of node -- the caller makes a decision based on whether the node was added
                                     * or not and that decision should be made **once**.
                                     * To be more precise, the caller of ChildCompletion, in case when it's the last child,
                                     * should make a decision whether to start transition to the final state, based on
                                     * 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