History

avformat/libsrt: fix race condition with libsrt_network_wait_fd and epoll

The way SRT's async / epoll-based IO works is that the event status is stored in the epoll containers. That is, if an event occurs on an SRT socket, and that SRT socket isn't part of any epoll container, then that event is lost. If we later add that socket to an epoll container, we still won't receive the event even if it wasn't serviced. Therefore we create the epoll and put the fd into it right after the connection is established. See http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2021-January/275334.html

Marton Balint committed 5 years ago

Commit cae57ca1 parent 5cf01ed1 file · +39 −24Browse files

Unified · Split

libavformat/libsrt.c+39−24
@@ -163,15 +163,25 @@ static int libsrt_socket_nonblock(int socket, int enable) · 162 unmodified lines
163 return srt_setsockopt(socket, 0, SRTO_RCVSYN, &blocking, sizeof(blocking));163 return srt_setsockopt(socket, 0, SRTO_RCVSYN, &blocking, sizeof(blocking));
164}164}
165 165
166static int libsrt_network_wait_fd(URLContext *h, int eid, int fd, int write)166static int libsrt_epoll_create(URLContext *h, int fd, int write)
167{167{
168 int ret, len = 1, errlen = 1;
169 int modes = SRT_EPOLL_ERR | (write ? SRT_EPOLL_OUT : SRT_EPOLL_IN);168 int modes = SRT_EPOLL_ERR | (write ? SRT_EPOLL_OUT : SRT_EPOLL_IN);
169 int eid = srt_epoll_create();
170 if (eid < 0)
171 return libsrt_neterrno(h);
172 if (srt_epoll_add_usock(eid, fd, &modes) < 0) {
173 srt_epoll_release(eid);
174 return libsrt_neterrno(h);
175 }
176 return eid;
177}
178
179static int libsrt_network_wait_fd(URLContext *h, int eid, int write)
180{
181 int ret, len = 1, errlen = 1;
170 SRTSOCKET ready[1];182 SRTSOCKET ready[1];
171 SRTSOCKET error[1];183 SRTSOCKET error[1];
172 184
173 if (srt_epoll_add_usock(eid, fd, &modes) < 0)
174 return libsrt_neterrno(h);
175 if (write) {185 if (write) {
176 ret = srt_epoll_wait(eid, error, &errlen, ready, &len, POLLING_TIME, 0, 0, 0, 0);186 ret = srt_epoll_wait(eid, error, &errlen, ready, &len, POLLING_TIME, 0, 0, 0, 0);
177 } else {187 } else {
@@ -185,14 +195,12 @@ static int libsrt_network_wait_fd(URLContext *h, int eid, int fd, int write) · 7 unmodified lines
185 } else {195 } else {
186 ret = errlen ? AVERROR(EIO) : 0;196 ret = errlen ? AVERROR(EIO) : 0;
187 }197 }
188 if (srt_epoll_remove_usock(eid, fd) < 0)
189 return libsrt_neterrno(h);
190 return ret;198 return ret;
191}199}
192 200
193/* TODO de-duplicate code from ff_network_wait_fd_timeout() */201/* TODO de-duplicate code from ff_network_wait_fd_timeout() */
194 202
195static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)203static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int write, int64_t timeout, AVIOInterruptCB *int_cb)
196{204{
197 int ret;205 int ret;
198 int64_t wait_start = 0;206 int64_t wait_start = 0;
@@ -200,7 +208,7 @@ static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int fd, int wr · 1 unmodified line
200 while (1) {208 while (1) {
201 if (ff_check_interrupt(int_cb))209 if (ff_check_interrupt(int_cb))
202 return AVERROR_EXIT;210 return AVERROR_EXIT;
203 ret = libsrt_network_wait_fd(h, eid, fd, write);211 ret = libsrt_network_wait_fd(h, eid, write);
204 if (ret != AVERROR(EAGAIN))212 if (ret != AVERROR(EAGAIN))
205 return ret;213 return ret;
206 if (timeout > 0) {214 if (timeout > 0) {
@@ -225,7 +233,7 @@ static int libsrt_listen(int eid, int fd, const struct sockaddr *addr, socklen_t · 18 unmodified lines
225 if (srt_listen(fd, 1))233 if (srt_listen(fd, 1))
226 return libsrt_neterrno(h);234 return libsrt_neterrno(h);
227 235
228 ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback);236 ret = libsrt_network_wait_fd_timeout(h, eid, 1, timeout, &h->interrupt_callback);
229 if (ret < 0)237 if (ret < 0)
230 return ret;238 return ret;
231 239
@@ -245,7 +253,7 @@ static int libsrt_listen_connect(int eid, int fd, const struct sockaddr *addr, s · 13 unmodified lines
245 if (srt_connect(fd, addr, addrlen) < 0)253 if (srt_connect(fd, addr, addrlen) < 0)
246 return libsrt_neterrno(h);254 return libsrt_neterrno(h);
247 255
248 ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback);256 ret = libsrt_network_wait_fd_timeout(h, eid, 1, timeout, &h->interrupt_callback);
249 if (ret < 0) {257 if (ret < 0) {
250 if (will_try_next) {258 if (will_try_next) {
251 av_log(h, AV_LOG_WARNING,259 av_log(h, AV_LOG_WARNING,
@@ -359,7 +367,7 @@ static int libsrt_set_options_pre(URLContext *h, int fd) · 107 unmodified lines
359static int libsrt_setup(URLContext *h, const char *uri, int flags)367static int libsrt_setup(URLContext *h, const char *uri, int flags)
360{368{
361 struct addrinfo hints = { 0 }, *ai, *cur_ai;369 struct addrinfo hints = { 0 }, *ai, *cur_ai;
362 int port, fd = -1;370 int port, fd;
363 SRTContext *s = h->priv_data;371 SRTContext *s = h->priv_data;
364 const char *p;372 const char *p;
365 char buf[256];373 char buf[256];
@@ -367,7 +375,7 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 1 unmodified line
367 char hostname[1024],proto[1024],path[1024];375 char hostname[1024],proto[1024],path[1024];
368 char portstr[10];376 char portstr[10];
369 int64_t open_timeout = 0;377 int64_t open_timeout = 0;
370 int eid;378 int eid, write_eid;
371 379
372 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),380 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
373 &port, path, sizeof(path), uri);381 &port, path, sizeof(path), uri);
@@ -404,11 +412,6 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 30 unmodified lines
404 412
405 cur_ai = ai;413 cur_ai = ai;
406 414
407 eid = srt_epoll_create();
408 if (eid < 0)
409 return libsrt_neterrno(h);
410 s->eid = eid;
411
412 restart:415 restart:
413 416
414 fd = srt_socket(cur_ai->ai_family, cur_ai->ai_socktype, 0);417 fd = srt_socket(cur_ai->ai_family, cur_ai->ai_socktype, 0);
@@ -432,9 +435,14 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 17 unmodified lines
432 if (libsrt_socket_nonblock(fd, 1) < 0)435 if (libsrt_socket_nonblock(fd, 1) < 0)
433 av_log(h, AV_LOG_DEBUG, "libsrt_socket_nonblock failed\n");436 av_log(h, AV_LOG_DEBUG, "libsrt_socket_nonblock failed\n");
434 437
438 ret = write_eid = libsrt_epoll_create(h, fd, 1);
439 if (ret < 0)
440 goto fail1;
435 if (s->mode == SRT_MODE_LISTENER) {441 if (s->mode == SRT_MODE_LISTENER) {
436 // multi-client442 // multi-client
437 if ((ret = libsrt_listen(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen, h, s->listen_timeout)) < 0)443 ret = libsrt_listen(write_eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen, h, s->listen_timeout);
444 srt_epoll_release(write_eid);
445 if (ret < 0)
438 goto fail1;446 goto fail1;
439 srt_close(fd);447 srt_close(fd);
440 fd = ret;448 fd = ret;
@@ -442,12 +450,15 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 1 unmodified line
442 if (s->mode == SRT_MODE_RENDEZVOUS) {450 if (s->mode == SRT_MODE_RENDEZVOUS) {
443 if (srt_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) {451 if (srt_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) {
444 ret = libsrt_neterrno(h);452 ret = libsrt_neterrno(h);
453 srt_epoll_release(write_eid);
445 goto fail1;454 goto fail1;
446 }455 }
447 }456 }
448 457
449 if ((ret = libsrt_listen_connect(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen,458 ret = libsrt_listen_connect(write_eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
450 open_timeout, h, !!cur_ai->ai_next)) < 0) {459 open_timeout, h, !!cur_ai->ai_next);
460 srt_epoll_release(write_eid);
461 if (ret < 0) {
451 if (ret == AVERROR_EXIT)462 if (ret == AVERROR_EXIT)
452 goto fail1;463 goto fail1;
453 else464 else
@@ -468,8 +479,13 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 14 unmodified lines
468 h->max_packet_size = packet_size;479 h->max_packet_size = packet_size;
469 }480 }
470 481
482 ret = eid = libsrt_epoll_create(h, fd, flags & AVIO_FLAG_WRITE);
483 if (eid < 0)
484 goto fail1;
485
471 h->is_streamed = 1;486 h->is_streamed = 1;
472 s->fd = fd;487 s->fd = fd;
488 s->eid = eid;
473 489
474 freeaddrinfo(ai);490 freeaddrinfo(ai);
475 return 0;491 return 0;
@@ -487,7 +503,6 @@ static int libsrt_setup(URLContext *h, const char *uri, int flags) · 11 unmodified lines
487 if (fd >= 0)503 if (fd >= 0)
488 srt_close(fd);504 srt_close(fd);
489 freeaddrinfo(ai);505 freeaddrinfo(ai);
490 srt_epoll_release(s->eid);
491 return ret;506 return ret;
492}507}
493 508
@@ -644,7 +659,7 @@ static int libsrt_read(URLContext *h, uint8_t *buf, int size) · 150 unmodified lines
644 int ret;659 int ret;
645 660
646 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {661 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
647 ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 0, h->rw_timeout, &h->interrupt_callback);662 ret = libsrt_network_wait_fd_timeout(h, s->eid, 0, h->rw_timeout, &h->interrupt_callback);
648 if (ret)663 if (ret)
649 return ret;664 return ret;
650 }665 }
@@ -663,7 +678,7 @@ static int libsrt_write(URLContext *h, const uint8_t *buf, int size) · 12 unmodified lines
663 int ret;678 int ret;
664 679
665 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {680 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
666 ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 1, h->rw_timeout, &h->interrupt_callback);681 ret = libsrt_network_wait_fd_timeout(h, s->eid, 1, h->rw_timeout, &h->interrupt_callback);
667 if (ret)682 if (ret)
668 return ret;683 return ret;
669 }684 }
@@ -680,8 +695,8 @@ static int libsrt_close(URLContext *h) · 10 unmodified lines
680{695{
681 SRTContext *s = h->priv_data;696 SRTContext *s = h->priv_data;
682 697
683 srt_close(s->fd);
684 srt_epoll_release(s->eid);698 srt_epoll_release(s->eid);
699 srt_close(s->fd);
685 700
686 srt_cleanup();701 srt_cleanup();
687 702

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t