From a6dd780694f26f2e414c7392f1caa47efb9d71a5 Mon Sep 17 00:00:00 2001
From: Kirill Smelkov <kirr@nexedi.com>
Date: Mon, 16 Sep 2024 15:33:26 +0300
Subject: [PATCH] wcfs: Fix potential stuck in WatchLink.serve exit codepath

When serve is completing and going to exit, it sends an error message to
the client without any timeout. So if the client is not reading from the
channel, wcfs will get stuck waiting for the message to be consumed.

-> Fix that by trying to send that last error only during 1 second and
   ignoring errors if any

Test is, hopefully, TODO.

/reviewed-by @levin.zimmermann
/reviewed-on https://lab.nexedi.com/nexedi/wendelin.core/-/merge_requests/18
---
 wcfs/wcfs.go | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/wcfs/wcfs.go b/wcfs/wcfs.go
index cc3864b..9635073 100644
--- a/wcfs/wcfs.go
+++ b/wcfs/wcfs.go
@@ -1899,9 +1899,11 @@ func (wlink *WatchLink) _serve() (err error) {
 		delete(head.wlinkTab, wlink)
 		head.wlinkMu.Unlock()
 
-		// write to peer if it was logical error on client side
+		// give client a chance to be notified if it was due to some logical error
 		if err != nil {
-			_ = wlink.send(ctx0, 0, fmt.Sprintf("error: %s", err))
+			ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
+			defer cancel()
+			_ = wlink.send(ctx, 0, fmt.Sprintf("error: %s", err))
 		}
 
 		// close .sk
-- 
2.30.9