This does not achieve the same result that I'm aiming for. When some other piece of code invoke this and get a resulting socket and throws an error (maybe not related to the socket), then the error gets caught by the except clause and discarded away. The socket shutdown/close code following "yield s" never gets executed and the socket doesn't gets cleaned up. This change a) defeats the purpose of this function and b) obscure any exception that should be propagated upwards.