When the Go driver detects that a context is cancelled, I think it will idle the connection and return it to the pool. Does that imply the running DB operation that it started will also be killed on the server? Or does that continue on?
You can read the official documentation about how the driver uses the context here.
Relevant section: Socket Read and Write:
When the driver retrieves a connection for an operation, it sets the socket’s read or write deadline to either the Context deadline or socket timeout, whichever is shorter.
If you cancel the Context after the execution of the
Read()orWrite()function but before its deadline, the behavior of the driver differs based on version.The driver generates a separate goroutine to listen for Context cancellation when the
Read()orWrite()function is in progress. If the goroutine detects a cancellation, it closes the connection. The pendingRead()orWrite()function returns an error which the driver overwrites with thecontext.Cancelederror.
Note that closing the connection just means putting it back to the pool as idle. There is no explict statement about cancelling the operation on the server, but obviously a signal is sent that the client abandons the operation and no longer monitors / cares for the result. The MongoDB server would be dumb not to abort the initiated operation if it can be. For example if the operation was a query, that can be aborted. If it was a write operation (such as insert or update), that may not be aborted.