diff --git a/codex/download.go b/codex/download.go index debe0d3..5794849 100644 --- a/codex/download.go +++ b/codex/download.go @@ -210,7 +210,11 @@ func (node CodexNode) DownloadStream(ctx context.Context, cid string, options Do return bridge.callError("cGoCodexDownloadLocal") } + // Create a done channel to signal the goroutine to stop + // when the download is complete and avoid goroutine leaks. done := make(chan struct{}) + defer close(done) + channelError := make(chan error, 1) go func() { select { @@ -222,7 +226,6 @@ func (node CodexNode) DownloadStream(ctx context.Context, cid string, options Do }() _, err = bridge.wait() - close(done) // Extract the potential cancellation error var cancelError error diff --git a/codex/upload.go b/codex/upload.go index 56455b5..4696721 100644 --- a/codex/upload.go +++ b/codex/upload.go @@ -305,7 +305,11 @@ func (node CodexNode) UploadFile(ctx context.Context, options UploadOptions) (st return "", bridge.callError("cGoCodexUploadFile") } + // Create a done channel to signal the goroutine to stop + // when the download is complete and avoid goroutine leaks. done := make(chan struct{}) + defer close(done) + channelError := make(chan error, 1) go func() { select { @@ -317,7 +321,6 @@ func (node CodexNode) UploadFile(ctx context.Context, options UploadOptions) (st }() _, err = bridge.wait() - close(done) // Extract the potential cancellation error var cancelErr error