Optimizing hangs sometimes

I am using the optimizing function in rstan, but it sometimes freezes, making me need to restart the R session. It will print the following before becoming unresponsive:

> optimfit_A1 <- optimizing(modelA, data=modelA_data, iter=5000, tol_obj=1e-13, tol_grad=1e-9, tol_rel_grad=1e3, tol_rel_obj=1e3, verbose = TRUE, save_iterations=TRUE, sample_file = 'samples.txt')
Initial log joint probability = -2.19565e+014
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes 

The exact same call will work 4 out of 5 times, but sometimes randomly hangs. I’m using a collection of similar models and a collection of similar data sets. So far it seems to happen more often with larger data sets or with “difficult” datasets where the optimal solution still doesn’t look very good.

How can I prevent this from happening?