fix deadlock when the crypto setup is closed while qtls writes messages

This commit is contained in:
Marten Seemann
2019-08-31 11:47:52 +07:00
parent f1d14ecdea
commit e224768e46

View File

@@ -70,7 +70,6 @@ type cryptoSetup struct {
runner handshakeRunner runner handshakeRunner
closed bool
alertChan chan uint8 alertChan chan uint8
// handshakeDone is closed as soon as the go routine running qtls.Handshake() returns // handshakeDone is closed as soon as the go routine running qtls.Handshake() returns
handshakeDone chan struct{} handshakeDone chan struct{}
@@ -283,19 +282,13 @@ func (h *cryptoSetup) RunHandshake() {
} }
func (h *cryptoSetup) onError(alert uint8, message string) { func (h *cryptoSetup) onError(alert uint8, message string) {
h.runner.OnError(qerr.CryptoError(alert, message)) h.runner.OnError(qerr.CryptoError(alert, message))
} }
// Close closes the crypto setup.
// It aborts the handshake, if it is still running.
// It must only be called once.
func (h *cryptoSetup) Close() error { func (h *cryptoSetup) Close() error {
h.mutex.Lock()
defer h.mutex.Unlock()
if h.closed {
return nil
}
h.closed = true
close(h.closeChan) close(h.closeChan)
// wait until qtls.Handshake() actually returned // wait until qtls.Handshake() actually returned
<-h.handshakeDone <-h.handshakeDone