[FIXED] Close() may not release resources immediately #370
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When a connection is closed very quickly after being created, there
was a race condition that could lead to the connection object still
being referenced (and therefore holding resources) until the first
ping interval elapsed.
This was because the pinger was created in spinGoRoutines() which
was itself created as a go routine. When the Close() call was made,
the ping timer may still be nil (therefore no way to stop it), but
then created in the go routine (which caused the reference to the
connection object). By default, it means that the connection object
would be held for 30 seconds.
Doing connect/close in a tight loop would show the process size
growing.
This PR refactors where we create the pinger and the use of the
wait group. There had been a change in a past to attempt to fix
a WaitGroup panic. I believe this approach is more stable. We do
create the pinger and bump the wait group in processConnectInit,
under connection lock. The doReconnect() go routine waits for
this wait group on entry and then on iterations where a failure
occurs after we know we have started the go routines.
In case of failures in the reconect loop, some state has also been
properly reset (namely the connection's buffered writer).
Resolves #368
Signed-off-by: Ivan Kozlovic [email protected]