go-libapns icon indicating copy to clipboard operation
go-libapns copied to clipboard

(Potential) race condition on close

Open joekarl opened this issue 10 years ago • 3 comments

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).

joekarl avatar Jan 12 '15 23:01 joekarl

Also while I'm at it, add documentation to attempt to read from the error channel for a bit after calling Disconnect()

joekarl avatar Jan 13 '15 00:01 joekarl

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

steeve avatar Nov 03 '15 10:11 steeve

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
                }
            }
        }

steeve avatar Nov 03 '15 10:11 steeve