Is it safe to use trace inside a STM stransaction?
Asked Answered
R

1

8

I have a transaction failing indefinitely for some reason, and I would like to use trace instructions inside. For example, to print the state of the MVar's before executing the transaction in this fragment:

    data_out <- atomically $ do 
        rtg_state <- takeTMVar ready_to_go 
        JobDescr hashid url <- T.readTBChan next_job_descr
        case rtg_state of 
            Ready_RTG n -> do
                putTMVar ready_to_go $ Processing_RTG n
                putTMVar start_harvester_browser hashid
                putTMVar next_test_url_to_check_chan  hashid
                putTMVar next_harvest_url hashid
                return (n,hashid,url)
            _ -> retry

Would that make the program segfault or miss-behave?

Retrad answered 1/6, 2015 at 8:51 Comment(0)
S
10

As long as you use trace for debug purposes only you should be OK. As a general rule, just assume that in the final production-ready version of your program there will be no traces around.

You will never observe segfaults from trace. Its "unsafety" stems from it injecting observable effects in pure code. E.g., in STM, when a transaction retries, its effects are assumed to be rolled back. If trace was used to send a message to the user, you can't roll that back. If the output of trace triggers a missile launch, you will have to deal with international side effects. If trace instead just signals the developer with "FYI, the code is doing X", this is not part of the core logic of the program, and is perfectly fine.

Selinaselinda answered 1/6, 2015 at 9:14 Comment(2)
The missile-launching code will be kept then in the IO monad. Thanks!Retrad
I find it worth pointing out that it's not a single missile launch that gives serious international side effects in this case – it's the 735 nuclear volleys when the transaction was retried a bunch of times you have to watch out for. ;)Selenium

© 2022 - 2024 — McMap. All rights reserved.