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

hPutStrLn is not atomic #200

Open
AndreasPK opened this issue Jan 25, 2020 · 2 comments · Fixed by #259
Open

hPutStrLn is not atomic #200

AndreasPK opened this issue Jan 25, 2020 · 2 comments · Fixed by #259

Comments

@AndreasPK
Copy link
Contributor

Since the newline is added in an additional hPut call another thread might write to the handle in the meantime.

I think that's acceptable but should be documented.

-- | Write a ByteString to a handle, appending a newline byte
hPutStrLn :: Handle -> ByteString -> IO ()
hPutStrLn h ps
    | length ps < 1024 = hPut h (ps `snoc` 0x0a)
    | otherwise        = hPut h ps >> hPut h (singleton (0x0a)) -- don't copy
@Bodigrim
Copy link
Contributor

Following https://www.snoyman.com/blog/2016/11/haskells-missing-concurrency-basics/, let's reopen this. I think the mitigation with length ps < 1024 = hPut h (ps snoc 0x0a) is both insufficient and inefficient.

@BebeSparkelSparkel
Copy link
Contributor

I am looking at the implementation of hPutStrLn and do not see an easy solution without copying. Perhaps, adding hPutStrLnAtomic is the solution.

A related discussion https://discourse.haskell.org/t/explain-ghc-internal-io-handle-text-hputstr/9389

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants