go-libapns
go-libapns copied to clipboard
(Potential) race condition on close
When Disconnect() is called, all that happens is the in flight buffer gets locked and then the socket is closed. The problem is if this is used in a multithreaded environment and there are any unsent payloads outside of the inflight buffer after Disconnect is called, the inflight buffer will never be flushed and there's a potential to lose whatever payload was being added as the connection was closed.
Probably just need to add an extra conditional here (https://github.com/joekarl/go-libapns/blob/master/connection.go#L299-L309) so that if the connection closed without an apple error, we return the unsent payload(s).
Also while I'm at it, add documentation to attempt to read from the error channel for a bit after calling Disconnect()
hey, any update on this? i've having an issue where UnsentPayloads.Len() == 0 when a wrong token is sent before a valid one. I think it may be due to that.
thanks for your hard work
note that the error code is 251, here is a snippet:
for {
fmt.Println("NEW CONNECTION")
apnsConn, _ := apns.NewAPNSConnection(&apns.APNSConfig{
CertificateBytes: certPem,
KeyBytes: keyPem,
})
loop:
for {
select {
case <-closing:
apnsConn.Disconnect()
return
case closeError := <-apnsConn.CloseChannel:
fmt.Println("ERROR !!!")
fmt.Println(closeError.Error.ErrorCode)
fmt.Println(closeError.UnsentPayloads.Len())
break loop
case p := <-c:
apnsConn.SendChannel <- p
}
}
}