Skip to content

Fix hGetContents to close handle on exception - #718

Open
Lysxia wants to merge 1 commit into
masterfrom
exception-getContents
Open

Lysxia wants to merge 1 commit into
masterfrom
exception-getContents

Conversation

@Lysxia

@Lysxia Lysxia commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Close #712

@Lysxia
Lysxia force-pushed the exception-getContents branch 2 times, most recently from f346371 to bdec0dd Compare October 25, 2026 21:15
Comment thread tests/Tests/Regressions.hs Outdated
hClose h0

-- readFile must close the handle on exception
let checkReadFile rf = do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please add a type signature for checkReadFile?

Comment thread src/Data/Text/IO.hs
Left e | Just ioe <- E.fromException e -> E.throwIO (augmentIOError ioe "hGetContents" h)
Left e -> E.throwIO
#if __GLASGOW_HASKELL__ >= 912
(E.NoBacktrace e)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we want no backtrace? (I'm rather illiterate when it comes to exceptions)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So we are rethrowing e :: SomeException, which already contains a backtrace. NoBacktrace tells throw not to add yet another backtrace on top.

This logic originates from System.IO.hGetContents' so I don't have all the rationale in mind. Notably I'm also curious why the first branch doesn't have NoBacktrace. Perhaps that's to remember where the extra data from augmentIOError comes from.

Another detail that makes investigating this difficult is there is a bug in 9.14 where throwIO discards any existing trace so I got no trace at all in the end. This has already been fixed upstream. So I'm planning to wait for the next GHC to have another look at how exceptions work.

@Lysxia
Lysxia force-pushed the exception-getContents branch from bdec0dd to e36b4c3 Compare October 28, 2026 00:26
@Lysxia
Lysxia force-pushed the exception-getContents branch from e36b4c3 to 7f0f8d5 Compare October 28, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hGetContents does not close handle on exception

2 participants