From 85be8690c0831a8036d1bc91d35b301404649046 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Tue, 13 Apr 2021 23:32:53 +0800 Subject: [PATCH] Fix: vmess aead writer concurrent write (#1350) --- component/vmess/aead.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/component/vmess/aead.go b/component/vmess/aead.go index 95478867..d4fbf2d9 100644 --- a/component/vmess/aead.go +++ b/component/vmess/aead.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "errors" "io" + "sync" "github.com/Dreamacro/clash/common/pool" ) @@ -15,6 +16,8 @@ type aeadWriter struct { nonce [32]byte count uint16 iv []byte + + writeLock sync.Mutex } func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter { @@ -22,8 +25,12 @@ func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter { } func (w *aeadWriter) Write(b []byte) (n int, err error) { + w.writeLock.Lock() buf := pool.Get(pool.RelayBufferSize) - defer pool.Put(buf) + defer func() { + w.writeLock.Unlock() + pool.Put(buf) + }() length := len(b) for { if length == 0 {