@@ -259,20 +259,29 @@ func (s *Shell) updateShellFromRunner(runner *interp.Runner) {
259259}
260260
261261// execCommon is the shared implementation for executing commands
262−func (s *Shell) execCommon(ctx context.Context, command string, stdout, stderr io.Writer) error {
262+func (s *Shell) execCommon(ctx context.Context, command string, stdout, stderr io.Writer) (err error) {
263+ var runner *interp.Runner
264+ defer func() {
265+ if r := recover(); r != nil {
266+ err = fmt.Errorf("command execution panic: %v", r)
267+ }
268+ if runner != nil {
269+ s.updateShellFromRunner(runner)
270+ }
271+ s.logger.InfoPersist("command finished", "command", command, "err", err)
272+ }()
273+
263274 line, err := syntax.NewParser().Parse(strings.NewReader(command), "")
264275 if err != nil {
265276 return fmt.Errorf("could not parse command: %w", err)
266277 }
267278
268− runner, err := s.newInterp(stdout, stderr)
279+ runner, err = s.newInterp(stdout, stderr)
269280 if err != nil {
270281 return fmt.Errorf("could not run command: %w", err)
271282 }
272283
273284 err = runner.Run(ctx, line)
274− s.updateShellFromRunner(runner)
275− s.logger.InfoPersist("command finished", "command", command, "err", err)
276285 return err
277286}
278287