Closed institutions are a thing. You can be in therapy under close supervision and unable to leave until it is safe to allow you to do so. Arguably that is better than therapy in prison because even with trained staff on site a prison is still a prison. You could say that closed institutions are functionally a form of prisons and you'd be right but the difference in motivations matters: mental institutions exist to provide mental support, prisons exist to lock people away.