@@ -84,7 +84,7 @@ func (ncp *NetworkCaptureProvider) CaptureNetworkPacket(ctx context.Context, fil
8484 }
8585 if stopTrace {
8686 ncp .l .Info ("Stopping netsh trace session before starting a new one" )
87- _ = ncp .stopNetworkCapture (ctx )
87+ _ = ncp .stopNetworkCapture ()
8888 }
8989
9090 captureFileName := ncp .Filename .String () + ".etl"
@@ -164,7 +164,7 @@ func (ncp *NetworkCaptureProvider) CaptureNetworkPacket(ctx context.Context, fil
164164 }
165165
166166 ncp .l .Info ("Stop netsh" )
167- if err := ncp .stopNetworkCapture (ctx ); err != nil {
167+ if err := ncp .stopNetworkCapture (); err != nil {
168168 ncp .l .Error ("Failed to stop netsh trace by 'netsh trace stop', will kill the process" , zap .Error (err ))
169169 _ = captureStartCmd .Process .Kill ()
170170 return fmt .Errorf ("netsh stop failed: Output: %s" , err )
@@ -204,10 +204,15 @@ func (ncp *NetworkCaptureProvider) needToStopTraceSession(ctx context.Context) (
204204 return false , fmt .Errorf ("cannot stop trace session because it's not created by Retina capture" )
205205}
206206
207- func (ncp * NetworkCaptureProvider ) stopNetworkCapture (ctx context. Context ) error {
207+ func (ncp * NetworkCaptureProvider ) stopNetworkCapture () error {
208208 ncp .l .Info ("Stopping netsh trace session" )
209209
210- command := exec .CommandContext (ctx , "netsh" , "trace" , "stop" )
210+ // Create independent context for cleanup.
211+ // netsh trace stop completes in ~1s; 30s timeout provides ample safety margin.
212+ stopCtx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
213+ defer cancel ()
214+
215+ command := exec .CommandContext (stopCtx , "netsh" , "trace" , "stop" )
211216 output , err := command .CombinedOutput ()
212217 // ignore the error when stop the trace when no live trace session exists.
213218 if strings .Contains (string (output ), "There is no trace session currently in progress" ) {
0 commit comments