From 4103542f090bad61ee000f0edb250c40d023228b Mon Sep 17 00:00:00 2001
From: Jens Nolte <git@queezle.net>
Date: Wed, 8 Dec 2021 20:15:14 +0100
Subject: [PATCH] Fix read on closed file descriptor

---
 src/Quasar/Wayland/Connection.hs | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/Quasar/Wayland/Connection.hs b/src/Quasar/Wayland/Connection.hs
index 4c91fb1..09b38d1 100644
--- a/src/Quasar/Wayland/Connection.hs
+++ b/src/Quasar/Wayland/Connection.hs
@@ -12,6 +12,8 @@ import Network.Socket qualified as Socket
 import Network.Socket.ByteString qualified as Socket
 import Network.Socket.ByteString.Lazy qualified as SocketL
 import Quasar
+-- TODO remove after updating quasar
+import Quasar.Async.Unmanaged (Async)
 import Quasar.Prelude
 import Quasar.Wayland.Protocol
 
@@ -49,15 +51,18 @@ newWaylandConnection initializeProtocolAction socket = do
       resourceManager
     }
 
-    registerDisposeAction $ closeConnection connection
+    t1 <- connectionThread connection $ sendThread connection
+    t2 <- connectionThread connection $ receiveThread connection
 
-    connectionThread connection $ sendThread connection
-    connectionThread connection $ receiveThread connection
+    registerAsyncDisposeAction do
+      await $ isDisposed t1
+      await $ isDisposed t2
+      closeConnection connection
 
     pure (result, connection)
 
-connectionThread :: MonadResourceManager m => WaylandConnection s -> IO () -> m ()
-connectionThread connection work = asyncWithHandler_ traceAndDisposeConnection $ liftIO $ work
+connectionThread :: MonadResourceManager m => WaylandConnection s -> IO () -> m (Async ())
+connectionThread connection work = asyncWithHandler traceAndDisposeConnection $ liftIO $ work
   where
     traceAndDisposeConnection :: SomeException -> IO ()
     traceAndDisposeConnection ex = traceIO (displayException ex) >> void (dispose connection)
@@ -74,7 +79,7 @@ receiveThread :: IsSide s => WaylandConnection s -> IO ()
 receiveThread connection = forever do
   bytes <- Socket.recv connection.socket 4096
 
-  when (BS.length bytes == 0) do
+  when (BS.null bytes) do
     throwM SocketClosed
 
   traceIO $ "Received " <> show (BS.length bytes) <> " bytes"
-- 
GitLab