<p>The <code>help</code> of the <code>kubectl port-forward</code> says <code>The forwarding session ends when the selected pod terminates, and rerun of the command is needed to resume forwarding.</code></p>
<p>Although it does not auto-reconnect when the pod terminates the command does not return either and just hangs with errors:</p>
<pre><code>E0929 11:57:50.187945   62466 portforward.go:400] an error occurred forwarding 8000 -&gt; 8080: error forwarding port 8080 to pod a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249, uid : network namespace for sandbox &quot;a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249&quot; is closed
Handling connection for 8000
E0929 12:02:44.505938   62466 portforward.go:400] an error occurred forwarding 8000 -&gt; 8080: error forwarding port 8080 to pod a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249, uid : failed to find sandbox &quot;a1fe1d167955e1c345e0f8026c4efa70a84b9d46029037ebc5b69d9da5d30249&quot; in store: not found
</code></pre>
<p>I would like it to return so that I can handle this error and make the script that will rerun it.</p>
<p>Is there any way or workaround for how to do it?</p>

Sergii Bishyr
<p>Based on the information, described on <a href="https://github.com/kubernetes/kubernetes/issues/67059" rel="nofollow noreferrer">Kubernetes Issues page on GitHub</a>, I can suppose that it is a normal behavior for your case: port-forward connection cannot be canceled on pod deletion, since there is no a connection management inside REST connectors on server side.</p>
<blockquote>
<p>A connection being maintained from kubectl all the way through to the kubelet hanging open even if the pod doesn't exist.</p>
</blockquote>
<blockquote>
<p>We'll proxy a websocket connection kubectl-&gt;kubeapiserver-&gt;kubelet on port-forward.</p>
</blockquote>
