lib.ExecContext, lib.FileContext: Fix temp file leak and log messages #86

Merged
Jan Lindemann merged 3 commits from jan/fix/20260905-lib-execcontext-lib-filecontext-fix-temp-file-leak-and-log-messages into master 2026-09-05 19:45:29 +02:00 AGit

This PR adds three independent fixes to the ExecContext and FileContext helpers, each correcting a defect in temp file cleanup, error reporting, or debug logging. All have been found and fixed by AI.

lib.ExecContext: Clean up temp file in _put()

_put() writes the content into a temporary file with tee and, when atomic is set, moves it to the target path with a final mv. The loop over the command list sets tmp_file to None after each command, on the assumption stated in the comment that the file has been moved at that point - which is only true for the last command. When a chown, chmod, or mv after the tee fails, the finally block finds tmp_file is None and leaves the temporary file behind.

Reset tmp_file only after all commands have completed, so that the finally block erases the temporary file whenever a step fails.

lib.FileContext: Interpolate path in _is_dir()

_is_dir() logs a DEBUG message when _stat() is not implemented and it must guess from the trailing slash whether a path is a directory. The second part of that message is a plain string rather than an f-string, so '{path}' is logged verbatim instead of the path.

Make it an f-string so the path is interpolated.

lib.ExecContext: Fix error message in _put()

_put() catches a failure of the remote command sequence and logs "Failed to get from ", a phrase copied over from the get() path. This path, however, pushes content, so the message describes the wrong operation.

Log a put-oriented message instead.

This PR adds three independent fixes to the ExecContext and FileContext helpers, each correcting a defect in temp file cleanup, error reporting, or debug logging. All have been found and fixed by AI. #### lib.ExecContext: Clean up temp file in _put() _put() writes the content into a temporary file with tee and, when atomic is set, moves it to the target path with a final mv. The loop over the command list sets tmp_file to None after each command, on the assumption stated in the comment that the file has been moved at that point - which is only true for the last command. When a chown, chmod, or mv after the tee fails, the finally block finds tmp_file is None and leaves the temporary file behind. Reset tmp_file only after all commands have completed, so that the finally block erases the temporary file whenever a step fails. #### lib.FileContext: Interpolate path in _is_dir() _is_dir() logs a DEBUG message when _stat() is not implemented and it must guess from the trailing slash whether a path is a directory. The second part of that message is a plain string rather than an f-string, so '{path}' is logged verbatim instead of the path. Make it an f-string so the path is interpolated. #### lib.ExecContext: Fix error message in _put() _put() catches a failure of the remote command sequence and logs "Failed to get <path> from <root>", a phrase copied over from the get() path. This path, however, pushes content, so the message describes the wrong operation. Log a put-oriented message instead.
_put() writes the content into a temporary file with tee and, when
atomic is set, moves it to the target path with a final mv. The loop
over the command list sets tmp_file to None after each command, on the
assumption stated in the comment that the file has been moved at that
point - which is only true for the last command. When a chown, chmod,
or mv after the tee fails, the finally block finds tmp_file is None
and leaves the temporary file behind.

Reset tmp_file only after all commands have completed, so that the
finally block erases the temporary file whenever a step fails.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
_is_dir() logs a DEBUG message when _stat() is not implemented and it
must guess from the trailing slash whether a path is a directory. The
second part of that message is a plain string rather than an f-string,
so '{path}' is logged verbatim instead of the path.

Make it an f-string so the path is interpolated.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
lib.ExecContext: Fix error message in _put()
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m20s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m18s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 3m54s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 4m4s
CI / Packaging test (push) Successful in 0s
f6372d321a
_put() catches a failure of the remote command sequence and logs
"Failed to get <path> from <root>", a phrase copied over from the
get() path. This path, however, pushes content, so the message
describes the wrong operation.

Log a put-oriented message instead.

Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2
Signed-off-by: Jan Lindemann <jan@janware.com>
Jan Lindemann scheduled this pull request to auto merge when all checks succeed 2026-09-05 19:36:48 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
janware/jw-pkg!86
No description provided.