@@ -338,7 +338,6 @@ func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *For
338338 } else {
339339 // we have length information, let's download by chunks!
340340 c .downloadChunked (ctx , req , w , format )
341-
342341 }
343342
344343 return r , contentLength , nil
@@ -402,19 +401,21 @@ func (c *Client) downloadChunked(ctx context.Context, req *http.Request, w *io.P
402401 currentChunk := atomic.Uint32 {}
403402 for i := 0 ; i < maxRoutines ; i ++ {
404403 go func () {
405- i := int (currentChunk .Add (1 )) - 1
406- if i > len (chunks ) {
407- // no more chunks
408- return
409- }
404+ for {
405+ chunkIndex := int (currentChunk .Add (1 )) - 1
406+ if chunkIndex >= len (chunks ) {
407+ // no more chunks
408+ return
409+ }
410410
411- chunk := & chunks [i ]
412- err := c .downloadChunk (req .Clone (cancelCtx ), chunk )
413- close (chunk .data )
411+ chunk := & chunks [chunkIndex ]
412+ err := c .downloadChunk (req .Clone (cancelCtx ), chunk )
413+ close (chunk .data )
414414
415- if err != nil {
416- abort (err )
417- return
415+ if err != nil {
416+ abort (err )
417+ return
418+ }
418419 }
419420 }()
420421 }
@@ -424,6 +425,7 @@ func (c *Client) downloadChunked(ctx context.Context, req *http.Request, w *io.P
424425 for i := 0 ; i < len (chunks ); i ++ {
425426 select {
426427 case <- cancelCtx .Done ():
428+ abort (context .Canceled )
427429 return
428430 case data := <- chunks [i ].data :
429431 _ , err := io .Copy (w , bytes .NewBuffer (data ))
0 commit comments