diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index dcceb68a2554815d23e8e31a84ea3fc1fb7340cf..dddaaf42379d86ddfe1289be86ecb185d27559b5 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -1271,12 +1271,7 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
     return 0;
 
 fail:
-    for (i = 0; i < rt->nb_rtsp_streams; i++) {
-        if (rt->rtsp_streams[i]->rtp_handle) {
-            url_close(rt->rtsp_streams[i]->rtp_handle);
-            rt->rtsp_streams[i]->rtp_handle = NULL;
-        }
-    }
+    ff_rtsp_undo_setup(s);
     return err;
 }