Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use trace inside a STM stransaction?

Tags:

haskell

stm

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?

like image 436
dsign Avatar asked Jun 01 '15 08:06

dsign


1 Answers

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.

like image 150
chi Avatar answered Oct 21 '22 09:10

chi