janimo / textsecure

TextSecure client package for Go
GNU General Public License v3.0
129 stars 26 forks source link

network connection error after StopListening(), StartListening() #27

Closed abarisani closed 7 years ago

abarisani commented 8 years ago

Hello,

we are using your library on a running instance and we require to stop/start the listener at will, by re-using the same client (or even allocating new ones), we stumble upon the following issue on quick re-connections:

ERRO[2016/04/13 14:34:48] EOF
ERRO[2016/04/13 14:34:48] read tcp 192.168.0.101:59546->52.7.8.191:443: use of closed network connection ERRO[2016/04/13 14:35:03] crypto/tls: use of closed connection

I haven't pin pointed what is responsible for this but surely the following goroutine:

go wsconn.keepAlive()

maybe should not be started every time StartListening() is invoked as it's never cleaned up by StopListening().

However I don't think this is the cause of the error message given that we can't receive messages once the condition happens.

Do you have any suggestions on what my be the culprit here and how it can be fixed?

Thanks

janimo commented 8 years ago

Hi,

I'll look into this (I have not used StopListening() myself) How are your goroutines calling Start/Stop listening?

abarisani commented 8 years ago

We hold a single instance of *textsecure.Client and call its StartListening()/StopListening() without goroutines involved.

The scenario is a synchronous Start/Stop/Start sequence.

Thanks!

janimo commented 8 years ago

So only calling StopListening when StartListening has exited it's for loop? Is StartListening() returning the error?

abarisani commented 8 years ago

We don't know if/when StartListening has exited the loop.

This is the core issue here, we want to stop/start at will but there seems to be no way to cleanly do this. One would expect that StopListening() could be invoked after StartListening() to have it exit the loop, instead this doesn't happen cleanly as the errors I pasted suggest.

The errors are not given directly by StartListening() or StopListening() which both exist cleanly after invocation.

I hope this helps.

janimo commented 8 years ago

If there are no goroutines how do you run StopListening before StartListening exited? Is there a simplified test app that can reproduce the issue?

abarisani commented 8 years ago

Ah, you are right. Sorry for that, I do indeed start in a goroutine (and that is the only one involved):

// stuff

go func() { textsecure.StartListening() }

// stuff

textsecure.StopListening()

What is the best approach/hook to call StopListening() safely ?

Thanks