don't lock the client mutex when handling regular packets

This commit is contained in:
Marten Seemann
2018-11-27 13:39:44 +07:00
parent 38548c137c
commit a8db148dbf
2 changed files with 9 additions and 8 deletions

View File

@@ -27,7 +27,7 @@ type client struct {
token []byte token []byte
versionNegotiated bool // has the server accepted our version versionNegotiated utils.AtomicBool // has the server accepted our version
receivedVersionNegotiationPacket bool receivedVersionNegotiationPacket bool
negotiatedVersions []protocol.VersionNumber // the list of versions from the version negotiation packet negotiatedVersions []protocol.VersionNumber // the list of versions from the version negotiation packet
@@ -295,28 +295,29 @@ func (c *client) handlePacket(p *receivedPacket) {
} }
func (c *client) handlePacketImpl(p *receivedPacket) error { func (c *client) handlePacketImpl(p *receivedPacket) error {
c.mutex.Lock()
defer c.mutex.Unlock()
// handle Version Negotiation Packets // handle Version Negotiation Packets
if p.hdr.IsVersionNegotiation() { if p.hdr.IsVersionNegotiation() {
c.mutex.Lock()
err := c.handleVersionNegotiationPacket(p.hdr) err := c.handleVersionNegotiationPacket(p.hdr)
if err != nil { if err != nil {
c.session.destroy(err) c.session.destroy(err)
} }
c.mutex.Unlock()
// version negotiation packets have no payload // version negotiation packets have no payload
return err return err
} }
if p.hdr.Type == protocol.PacketTypeRetry { if p.hdr.Type == protocol.PacketTypeRetry {
c.mutex.Lock()
c.handleRetryPacket(p.hdr) c.handleRetryPacket(p.hdr)
c.mutex.Unlock()
return nil return nil
} }
// this is the first packet we are receiving // this is the first packet we are receiving
// since it is not a Version Negotiation Packet, this means the server supports the suggested version // since it is not a Version Negotiation Packet, this means the server supports the suggested version
if !c.versionNegotiated { if !c.versionNegotiated.Get() {
c.versionNegotiated = true c.versionNegotiated.Set(true)
} }
c.session.handlePacket(p) c.session.handlePacket(p)
@@ -325,7 +326,7 @@ func (c *client) handlePacketImpl(p *receivedPacket) error {
func (c *client) handleVersionNegotiationPacket(hdr *wire.Header) error { func (c *client) handleVersionNegotiationPacket(hdr *wire.Header) error {
// ignore delayed / duplicated version negotiation packets // ignore delayed / duplicated version negotiation packets
if c.receivedVersionNegotiationPacket || c.versionNegotiated { if c.receivedVersionNegotiationPacket || c.versionNegotiated.Get() {
c.logger.Debugf("Received a delayed Version Negotiation Packet.") c.logger.Debugf("Received a delayed Version Negotiation Packet.")
return nil return nil
} }

View File

@@ -687,7 +687,7 @@ var _ = Describe("Client", func() {
}, },
}) })
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(cl.versionNegotiated).To(BeTrue()) Expect(cl.versionNegotiated.Get()).To(BeTrue())
}) })
It("errors if no matching version is found", func() { It("errors if no matching version is found", func() {