You need to save the current solution, so you can recall in case of divergence
Ubck = Function(U)
while True:
try:
solver.solve()
break
except:
U.assign(Ubck)
dt.assign(0.5*float(dt))
This assumes that you have defined a solver object, which writes to U and throws an error in case of divergence.
This is a way. Maybe not the best way. At least you should also consider increasing the timestep.