Skip to content

Commit

Permalink
fix tlsStarting deadlock on concurrent operations
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinz01 committed May 3, 2024
1 parent 1ce656f commit aeb5e99
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Conn struct {
// TLS config for StartTLS connections
TLSConfig *tls.Config
// Mutex to prevent reading/writing while setting up TLS
tlsStarting sync.Mutex
tlsStarting sync.RWMutex
// Mutex to synchronize message sending
sending sync.Mutex
// Wait group to enable atomic Bind request processing
Expand Down Expand Up @@ -80,8 +80,8 @@ func (c *Conn) SendUnsolicitedNotification(resultCode LDAPResultCode, diagnostic

// Sends a LDAPMessage to the client and removes the corresponding message from the abandonment cache
func (c *Conn) SendMessage(msg *Message) error {
c.tlsStarting.Lock()
defer c.tlsStarting.Unlock()
c.tlsStarting.RLock()
defer c.tlsStarting.RUnlock()
c.sending.Lock()
defer c.sending.Unlock()
_, err := io.Copy(c.conn, bytes.NewReader(msg.EncodeWithHeader()))
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ func (s *LDAPServer) handleConnection(c net.Conn) {
// Close() called
return
}
ldapConn.tlsStarting.Lock()
ldapConn.tlsStarting.RLock()
msg, err := ldapConn.ReadMessage()
ldapConn.tlsStarting.Unlock()
ldapConn.tlsStarting.RUnlock()
if err != nil {
if errors.Is(err, syscall.Errno(0x2746)) { // Windows: An existing connection was forcibly closed by the client
log.Println("Connection was reset by the client.")
Expand Down

0 comments on commit aeb5e99

Please sign in to comment.