Skip to content

Added timeout handling in download - #19

Closed
cmaglie wants to merge 1 commit into
mainfrom
timeouts-2
Closed

cmaglie wants to merge 1 commit into
mainfrom
timeouts-2

Conversation

@cmaglie

@cmaglie cmaglie commented Jan 7, 2026

Copy link
Copy Markdown
Member

This PR implements an "inactivity" timeout that triggers if no network activity lasts for a certain period of time.

Comment thread downloader.go
Comment on lines +163 to +168
// Timeout setup
if config.InactivityTimeout > 0 {
timeoutCtx, cancel := context.WithTimeout(ctx, config.InactivityTimeout)
defer cancel()
ctx = timeoutCtx
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem an Inactivity timeout but rather a global timeout, is this a leftover?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the "connection" phase, during which we exchange basic headers.
In this phase, I opted for a global timeout, which is much simpler to implement (I'm not sure an inactivity timeout is even possible here).

@lucarin91 lucarin91 Jan 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, maybe we could update the comment

// setup a wall clock timeout during the connection phase.

Comment thread downloader.go
if err != nil {
d.err = err
d.lock.Lock()
if d.err == nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this code is actually working; the whole system is based on the assumption that a download that doesn't progress will continuously return 0, nil in the Read operation. AFAIK, this isn't always the case, and if the Read is blocked for the whole timeout, we have no way to exit with the timeout error. I think that we probably need to do the read loop in a go routine and use a select to do early return in case the read doesn't advance

@cmaglie cmaglie Jan 7, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole system is based on the assumption that a download that doesn't progress will continuously return 0, nil in the Read operation

No, the assumption is that closing an io.ReadCloser will unblock all pending Reads.
That's what the timer function does after a t.Timeout period, unless some bytes are read and the timer is Resetted.

At least this is the theory I didn't test this properly (yet).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, I missed the Close call in my review. Anyway, I am not sure we should rely on this. Does the Close operation require stopping all running Read operations? For instance, the user could wrap the reader in a NopCloser, which completely ignores the close operation. In the specific case of HTTP body reading, we may be safe, but we should double-check.

Base automatically changed from timeouts to main January 7, 2026 13:59
Comment thread downloader.go
d.lock.Lock()
if d.err == nil {
d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout)
in.Close()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case I would add a comment here

Suggested change
in.Close()
in.Close() // close the reader for stopping the download

Comment thread downloader.go
Comment on lines +84 to +95
var timeoutTimer *time.Timer
if d.timeout > 0 {
timeoutTimer = time.AfterFunc(d.timeout, func() {
d.lock.Lock()
if d.err == nil {
d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout)
in.Close()
}
d.lock.Unlock()
})
defer timeoutTimer.Stop()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can create an already triggered timer to avoid the nil check below

Suggested change
var timeoutTimer *time.Timer
if d.timeout > 0 {
timeoutTimer = time.AfterFunc(d.timeout, func() {
d.lock.Lock()
if d.err == nil {
d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout)
in.Close()
}
d.lock.Unlock()
})
defer timeoutTimer.Stop()
}
var timeoutTimer *time.Timer
if d.timeout > 0 {
timeoutTimer = time.AfterFunc(d.timeout, func() {
d.lock.Lock()
if d.err == nil {
d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout)
in.Close()
}
d.lock.Unlock()
})
defer timeoutTimer.Stop()
}else{
timeoutTimer = time.NewTimer(0)
}

@cmaglie

cmaglie commented Jan 8, 2026

Copy link
Copy Markdown
Member Author

Ok, I'm closing this one and making a new proposal.

@cmaglie cmaglie closed this Jan 8, 2026
@cmaglie
cmaglie deleted the timeouts-2 branch January 8, 2026 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants