Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent FdData leaks in getNotification #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Database/PostgreSQL/Simple/Notification.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Database.PostgreSQL.Simple.Notification
) where

import Control.Monad ( join, void )
import Control.Exception ( throwIO, catch )
import Control.Exception ( throwIO, onException, mapException )
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Database.PostgreSQL.Simple.Internal
Expand Down Expand Up @@ -120,9 +120,10 @@ getNotification conn = join $ withConnection conn fetch
-- the lock... but such a major bug is likely to exhibit
-- itself in an at least somewhat more dramatic fashion.)
Just fd -> do
(waitRead, _) <- threadWaitReadSTM fd
(waitRead, unregister) <- threadWaitReadSTM fd
return $ do
atomically waitRead `catch` (throwIO . setIOErrorLocation)
mapException setIOErrorLocation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not

atomically waitRead `catch` \e -> do
  unregister
  throwIO (setIOErrorLocation e)

or can unregister itself throw? Even then I wouldn't use mapException as it uses unsafePerformIO, and here it's not required.

Copy link
Author

@velveteer velveteer Sep 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested your suggestion but saw no change in the heap, I think because it's not catching the async exception. I can still try to avoid mapException if you think it's that contentious (even though its documentation suggests it's safe).

(atomically waitRead `onException` unregister)
loop
#endif

Expand Down