How to Hijack stdin,err,out, thread safely?

Hi, I'm currently writing a web app to check the spec of input code usging QuickCheck. quickCheck* functions writes results to stderr & stdout, but I don't want them written to the app's original stdout/err. (I'm using SafeHaskell features to avoid malicious code executed, so don't worry about that :-)) So I want to execute I/O action hijacking stdout/err in a thread-safe manner. For example, I need function like below: censorHandles :: IO a -> IO (a, String, String) censorHandles = ... or withStd :: Handle -> Handle -> Handle -> IO a -> IO a withStd = ... -- Hiromi ISHII konn.jinro@gmail.com

Hi, > > I'm currently writing a web app to check the spec of input code > usging QuickCheck. > quickCheck* functions writes results to stderr & stdout, but I > don't want them written to the app's original stdout/err. > (I'm using SafeHaskell features to avoid malicious code executed, > so don't worry about that :-)) > > So I want to execute I/O action hijacking stdout/err in a thread-safe manner. > For example, I need function like below: > > censorHandles :: IO a -> IO (a, String, String) > censorHandles = ... > > or > > withStd :: Handle -> Handle -> Handle -> IO a -> IO a > withStd = ... > > -- Hiromi ISHII > konn.jinro@gmail.com > > > > > > _______________________________________________ > Haskell-Cafe mailing
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 https://hackage.haskell.org/package/silently may be of interest to you. Although I do not now whether it is threadsafe. On 06/30/2015 08:27 PM, Hiromi ISHII wrote: list > Haskell-Cafe@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBCAAGBQJVkuDxAAoJEM0PYZBmfhoBZUIIALUtTNr760pmWUg1CNXmYAzc /0dh2vVY9aidf7FV9OGlKB2wif1JMLbU7jpY4d0KIkIO4idpMjJXVRjLwiGtJngz 8jwBj2A6Y/uhx3FelJmO7F4m+RdwMGOQHsxTrnZET1HoCy/B+yEftNWAKht+OCA0 UOzWWYlcb2Z+JcLGoSL5xiRjkDhFo6QpdyMAiE9hZ6LzbKG06lQCslz2pRCY2wSx fdPlWzZnZtZVIgpjICe7lrCCT6yynGO4mdPbu3fQ/b3g84qNlsVrpoaAXtOERy7W k5XjpLBcV0qv1+xjlpr6pviQPLRcpod2G8ZxvjuRdTpJXXkJV90nkcMVCPYv244= =yMaF -----END PGP SIGNATURE-----

Hi,
On 2015/07/01 3:33,Jonas Scholl
wrote:
https://hackage.haskell.org/package/silently may be of interest to you. Although I do not now whether it is threadsafe.
Thanks! I didn't know about this package. This package seems great, but after I did some experement, it turns out to be not thread-safe. If we apply capture or silence to time-consuming actions, it eats entire output to stdout even made by other thread. For example, following code does not output anything and captures the output by another thread: ```haskell main :: IO () main = do print =<< (replicateM_ 100 $ do threadDelay (10^5) putStrLn "Hey, I'm here!") `concurrently` (capture_ $ replicateM_ 100 $ do threadDelay (10^5) putStrLn "Hey, You're there!") ``` I'm now suspecting that implementing the thread-safe versions of silently / capture is impossible... -- Hiromi ISHII konn.jinro@gmail.com

On Tue, Jun 30, 2015 at 2:27 PM, Hiromi ISHII
quickCheck* functions writes results to stderr & stdout, but I don't want them written to the app's original stdout/err. (I'm using SafeHaskell features to avoid malicious code executed, so don't worry about that :-))
So I want to execute I/O action hijacking stdout/err in a thread-safe manner. For example, I need function like below:
Handles are process level, not thread level, in all supported operating systems. If something is writing to stderr then it is writing to a process level entity and there is no way to capture it at the level of a thread. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On 30/06/15 21:27, Hiromi ISHII wrote:
Hi,
I'm currently writing a web app to check the spec of input code usging QuickCheck. quickCheck* functions writes results to stderr & stdout, but I don't want them written to the app's original stdout/err. (I'm using SafeHaskell features to avoid malicious code executed, so don't worry about that :-))
It's not clear from your email whether you are concerned about the output of QuickCheck itself or the functions you're testing. In the former case, there is a way to get the output of QuickCheck as a String without printing it. Take a look at tasty-quickcheck's code for an example. Roman

On 2015/07/01 4:00, Brandon Allbery
wrote: Handles are process level, not thread level, in all supported operating systems. If something is writing to stderr then it is writing to a process level entity and there is no way to capture it at the level of a thread.
Oh, yes. I get it. So the way I wanted to take in is completely wrong.
On 2015/07/01 4:02, Roman Cheplyaka wrote:
It's not clear from your email whether you are concerned about the output of QuickCheck itself or the functions you're testing.
Sorry for my bad writing. I need the former.
In the former case, there is a way to get the output of QuickCheck as a String without printing it. Take a look at tasty-quickcheck's code for an example.
It seems I can work it out in the same way with tasty-qc. Thanks! -- Hiromi ISHII konn.jinro@gmail.com

On Wed, Jul 01, 2015 at 03:27:46AM +0900, Hiromi ISHII wrote:
Hi,
I'm currently writing a web app to check the spec of input code usging QuickCheck. quickCheck* functions writes results to stderr & stdout, but I don't want them written to the app's original stdout/err. (I'm using SafeHaskell features to avoid malicious code executed, so don't worry about that :-))
So I want to execute I/O action hijacking stdout/err in a thread-safe manner. For example, I need function like below:
censorHandles :: IO a -> IO (a, String, String) censorHandles = ...
or
withStd :: Handle -> Handle -> Handle -> IO a -> IO a withStd = ...
Just to make sure, you have set `chatty` to false and passed it to one of the other QuickCheck runners, right? http://hackage.haskell.org/package/QuickCheck-2.8.1/docs/Test-QuickCheck.htm... I haven't used it myself, but I was under the impression that's how one controls the use of stderr/stdout (well, at least stdout). /M -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus Do not meddle in the affairs of Wizards, for they are subtle and quick to anger. -- J.R.R Tolkien

On 2015/07/01 4:04, Magnus Therning wrote: Just to make sure, you have set `chatty` to false and passed it to one of the other QuickCheck runners, right?
http://hackage.haskell.org/package/QuickCheck-2.8.1/docs/Test-QuickCheck.htm...
I haven't used it myself, but I was under the impression that's how one controls the use of stderr/stdout (well, at least stdout).
I overlooked that configuration item. tasty-qc also uses this functionality and I've confirmed that this just works fine for me. Thanks! -- Hiromi ISHII konn.jinro@gmail.com
participants (5)
-
Brandon Allbery
-
Hiromi ISHII
-
Jonas Scholl
-
Magnus Therning
-
Roman Cheplyaka