A stack overflow is basically caused by never letting the stack unwind properly. It does this when it reaches the end of an automation, the end of a thread off of a loop component, amongst other methods. I suspect you are simply using circular logic somewhere and never reaching a point where the stack can unwind. Be sure that however you are looping, you are making use of the loop components (ListLoop or ForLoop) as the end of an execution path from one of those will unwind the stack and allow the next execution to have a clean stack to add to.
If you need to do an infinite loop (i.e. a loop where you have no idea how many times it needs to run, but during execution, you can determine if this is the last record and thus break the loop), you can use a ForLoop. Leave the Initial to 0 and set the Limit to -1. This will effectively cause the loop to go on until it gets broken (Break method is called).