All chains finished unexpectedly!

I’m using CmdStanr to do my modelling. Inside my stan file (functions block), I have a custom function which I call inside the transformed parameters block to generate a parameter. Normally the sampling procedure of my model works fine with no problem. But whenever I want to print a real value in my custom function (for debugging), the sampling stops after a while, telling me: “No chains finished successfully. Unable to retrieve the draws”. If I remove the print line again, everything goes back to normal. It looks like a bug, but I’m not sure. My sampler is running on 4 parallel_chains. When I run in on 4 normal chains, only the last chain finishes unexpectedly.

I remember there used to be a horrifyingly bad practice for the multi-threading procedure in which treads had some wait-timer to communicate (instead of status checking). If one thread couldn’t finish fast enough, everything would go wrong. Since the print function introduces some delay in the function, I’m suspicious that: 1- the error I’m receiving is due to bad threading, 2- or at some point, the printing function somehow receives a value which can’t print, and my custom function exits without a return value. I appreciate any suggestion regarding this issue.

Have you tried just running on one chain? You don’t even have to set a lot of iterations just let it run for 100 or so.

When I run on 1 chain, it fails:

Warning message:
"Chain 1 finished unexpectedly!
Warning message:
“No chains finished successfully. Unable to retrieve the fit.”

And again, when I remove the print function, it works fine. So it can’t be the threading issue… strange! The print function is print(some real number)?

Could you make the model small enough to reproduce the error? For example just the function with the skeleton model? Or if your model is not too large could you share the code to see what’s going on? Currently, it might be a bit difficult to tell what’s wrong.