Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove log files if interrupted via Ctrl-C #235

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 93 additions & 84 deletions src/con_duct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,93 +1021,102 @@
files_to_close.append(report.usage_file)

report.start_time = time.time()
try:
report.process = process = subprocess.Popen(
[str(args.command)] + args.command_args,
stdout=stdout_file,
stderr=stderr_file,
start_new_session=True,
)
except FileNotFoundError:
# We failed to execute due to file not found in PATH
# We should remove log etc files since they are 0-sized
# degenerates etc
safe_close_files(files_to_close)
remove_files(log_paths, assert_empty=True)
# mimicking behavior of bash and zsh.
print(f"{args.command}: command not found", file=sys.stderr)
return 127 # seems what zsh and bash return then

lgr.info("duct is executing %r...", full_command)
lgr.info("Log files will be written to %s", log_paths.prefix)
try:
report.session_id = os.getsid(process.pid) # Get session ID of the new process
except ProcessLookupError: # process has already finished
# TODO: log this at least.
pass
stop_event = threading.Event()
if args.record_types.has_processes_samples():
monitoring_args = [
report,
process,
args.report_interval,
args.sample_interval,
stop_event,
]
monitoring_thread = threading.Thread(
target=monitor_process, args=monitoring_args
)
monitoring_thread.start()
else:
monitoring_thread = None
try: # Ctrl-C handling
try:
report.process = process = subprocess.Popen(
[str(args.command)] + args.command_args,
stdout=stdout_file,
stderr=stderr_file,
start_new_session=True,
)
except FileNotFoundError:
# We failed to execute due to file not found in PATH
# We should remove log etc files since they are 0-sized
# degenerates etc
safe_close_files(files_to_close)
remove_files(log_paths, assert_empty=True)
# mimicking behavior of bash and zsh.
print(f"{args.command}: command not found", file=sys.stderr)
return 127 # seems what zsh and bash return then

lgr.info("duct is executing %r...", full_command)
lgr.info("Log files will be written to %s", log_paths.prefix)
try:
report.session_id = os.getsid(
process.pid
) # Get session ID of the new process
except ProcessLookupError: # process has already finished

Check warning on line 1048 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1048

Added line #L1048 was not covered by tests
# TODO: log this at least.
pass

Check warning on line 1050 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1050

Added line #L1050 was not covered by tests
stop_event = threading.Event()
if args.record_types.has_processes_samples():
monitoring_args = [
report,
process,
args.report_interval,
args.sample_interval,
stop_event,
]
monitoring_thread = threading.Thread(
target=monitor_process, args=monitoring_args
)
monitoring_thread.start()
else:
monitoring_thread = None

Check warning on line 1065 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1065

Added line #L1065 was not covered by tests

if args.record_types.has_system_summary():
env_thread = threading.Thread(target=report.collect_environment)
env_thread.start()
sys_info_thread = threading.Thread(target=report.get_system_info)
sys_info_thread.start()
else:
env_thread, sys_info_thread = None, None

process.wait()
report.end_time = time.time()
lgr.debug("Process ended, setting stop_event to stop monitoring thread")
stop_event.set()
if monitoring_thread is not None:
lgr.debug("Waiting for monitoring thread to finish")
monitoring_thread.join()
lgr.debug("Monitoring thread finished")

# If we have any extra samples that haven't been written yet, do it now
if report.current_sample is not None:
report.write_subreport()

report.process = process
if env_thread is not None:
lgr.debug("Waiting for environment collection thread to finish")
env_thread.join()
lgr.debug("Environment collection finished")

if sys_info_thread is not None:
lgr.debug("Waiting for system information collection thread to finish")
sys_info_thread.join()
lgr.debug("System information collection finished")

if args.record_types.has_system_summary():
with open(log_paths.info, "w") as system_logs:
report.run_time_seconds = f"{report.end_time - report.start_time}"
system_logs.write(report.dump_json())
safe_close_files(files_to_close)
if process.returncode != 0 and (
report.elapsed_time < args.fail_time or args.fail_time < 0
):
lgr.info(
"Removing log files since command failed%s.",
f" in less than {args.fail_time} seconds" if args.fail_time > 0 else "",
)
if args.record_types.has_system_summary():
env_thread = threading.Thread(target=report.collect_environment)
env_thread.start()
sys_info_thread = threading.Thread(target=report.get_system_info)
sys_info_thread.start()
else:
env_thread, sys_info_thread = None, None

Check warning on line 1073 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1073

Added line #L1073 was not covered by tests

process.wait()
report.end_time = time.time()
lgr.debug("Process ended, setting stop_event to stop monitoring thread")
stop_event.set()
if monitoring_thread is not None:
lgr.debug("Waiting for monitoring thread to finish")
monitoring_thread.join()
lgr.debug("Monitoring thread finished")

# If we have any extra samples that haven't been written yet, do it now
if report.current_sample is not None:
report.write_subreport()

report.process = process
if env_thread is not None:
lgr.debug("Waiting for environment collection thread to finish")
env_thread.join()
lgr.debug("Environment collection finished")

if sys_info_thread is not None:
lgr.debug("Waiting for system information collection thread to finish")
sys_info_thread.join()
lgr.debug("System information collection finished")

if args.record_types.has_system_summary():
with open(log_paths.info, "w") as system_logs:
report.run_time_seconds = f"{report.end_time - report.start_time}"
system_logs.write(report.dump_json())
safe_close_files(files_to_close)
except KeyboardInterrupt:
lgr.info("Received Ctrl-C, removing log files")
safe_close_files(files_to_close)

Check warning on line 1106 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1104-L1106

Added lines #L1104 - L1106 were not covered by tests
remove_files(log_paths)
raise

Check warning on line 1108 in src/con_duct/__main__.py

View check run for this annotation

Codecov / codecov/patch

src/con_duct/__main__.py#L1108

Added line #L1108 was not covered by tests
else:
lgr.info(report.execution_summary_formatted)
if process.returncode != 0 and (
report.elapsed_time < args.fail_time or args.fail_time < 0
):
lgr.info(
"Removing log files since command failed%s.",
f" in less than {args.fail_time} seconds" if args.fail_time > 0 else "",
)
remove_files(log_paths)
else:
lgr.info(report.execution_summary_formatted)
return report.process.returncode


Expand Down
Loading