azihassan / youtube-d

A fast command-line Youtube downloader
MIT License
6 stars 0 forks source link

The process stops abruptly #39

Closed azihassan closed 11 months ago

azihassan commented 11 months ago

Even though the code is written to gracefully handle errors, the process will sometimes silently exit without a stack trace.

azihassan commented 11 months ago

Running it with strace reveals a concerning message :

poll([{fd=10, events=POLLIN}], 1, 1000 <unfinished ...>) = ?
+++ killed by SIGPIPE +++
azihassan commented 11 months ago

A few lines above, we have this :

poll([{fd=10, events=POLLIN}], 1, 1000) = 1 ([{fd=10, revents=POLLIN}])
poll([{fd=10, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1, 0) = 1 ([{fd=10, revents=POLLIN|POLLRDNORM}])
read(10, "\16\370\320\212,J \16\313N\331\252~P\204\347\214\"g\377tV\241\207\303\21P\311# \364\254"..., 15649) = 1368
read(10, 0x55eaf6330e20, 14281)         = -1 EAGAIN (Resource temporarily unavailable)

10 seems to be the file descriptor of the underlying cURL socket :

poll(NULL, 0, 4)                        = 0 (Timeout)
socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 10
setsockopt(10, SOL_TCP, TCP_NODELAY, [1], 4) = 0
fcntl(10, F_GETFL)                      = 0x2 (flags O_RDWR)
fcntl(10, F_SETFL, O_RDWR|O_NONBLOCK)   = 0
connect(10, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("105.73.187.12")}, 16) = -1 EINPROGRESS (Operation now in progress)

This would mean that Youtube closes the connection before the download is finished, or that youtube-d tries to read more data than what it's supposed to due to a range miscalculation or something like that.

azihassan commented 11 months ago

Relevant issue : https://github.com/googleapis/google-cloud-cpp/issues/2654

azihassan commented 11 months ago

Capturing and ignoring sigpipe seems to do the trick. The printf message doesn't get displayed, I suspect that it gets overwritten by one of the writeln("\r"... calls. But there has been no crashes since. Instead, it goes into timeout and the retry mechanism takes it up from there. I'll test it for a few more days to make sure that it works before merging.

Edit : found one

image