A command hook with the "finish" state gets run even when "borg --version" fails #1100

Closed
opened 2025-05-25 02:40:28 +00:00 by witten · 2 comments
Owner

What I'm trying to do and why

I'm trying to repro #1099, so I can update the docs accordingly.

Steps to reproduce

I created a configuration file with command hooks as follows:

commands:
  - after: configuration
    run:
      - "echo '*** start services'"
  - after: configuration
    run:
      - "echo '*** service remote backup'"
    states:
      - finish

Then to test the failure case, I intentionally used a non-existent local_path:

local_path: borgnoexist

Actual behavior

When I ran borgmatic, and the expected error occurred, the command hook that was restricted to the finish state still ran:

# borgmatic -c test.yaml create -v 2 --list 
/root/tmp/test.yaml: BORG_PASSPHRASE_FD=*** BORG_RELOCATED_REPO_ACCESS_IS_OK=*** BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=*** BORG_USE_CHUNKS_ARCHIVE=*** BORG_EXIT_CODES=*** borgnoexist --version --debug --show-rc
/root/tmp/test.yaml: /root/tmp/test.yaml: Error getting local Borg version
/root/tmp/test.yaml: [Errno 2] No such file or directory: 'borgnoexist'
/root/tmp/test.yaml: Running after configuration command hook
/root/tmp/test.yaml: echo '*** start services'
*** start services
/root/tmp/test.yaml: Running after configuration command hook
/root/tmp/test.yaml: echo '*** service remove backup'
*** service remove backup
/root/tmp/test.yaml: An error occurred

summary:
/root/tmp/test.yaml: Loading configuration file
An error occurred
/root/tmp/test.yaml: Error getting local Borg version
[Errno 2] No such file or directory: 'borgnoexist'

Need some help? https://torsion.org/borgmatic/#issues

Expected behavior

Any finish state command hooks should not run when an error occurs.

Other notes / implementation ideas

Fortunately, this bug appears to be restricted to the specific case of borg --version failing. That's because when such an error occurs in run_configuration(), there's a return instead of a raise. And therefore the state gets considered a finish instead of a fail. The fix is probably to change that return to a raise.

borgmatic version

main

borgmatic installation method

pipx install --editable

Borg version

1.4.1

Python version

3.13.3

Database version (if applicable)

No response

Operating system and version

Manjaro stable

### What I'm trying to do and why I'm trying to repro #1099, so I can update the docs accordingly. ### Steps to reproduce I created a configuration file with command hooks as follows: ```yaml commands: - after: configuration run: - "echo '*** start services'" - after: configuration run: - "echo '*** service remote backup'" states: - finish ``` Then to test the failure case, I intentionally used a non-existent `local_path`: ```yaml local_path: borgnoexist ``` ### Actual behavior When I ran borgmatic, and the expected error occurred, the command hook that was restricted to the `finish` state still ran: ``` # borgmatic -c test.yaml create -v 2 --list /root/tmp/test.yaml: BORG_PASSPHRASE_FD=*** BORG_RELOCATED_REPO_ACCESS_IS_OK=*** BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=*** BORG_USE_CHUNKS_ARCHIVE=*** BORG_EXIT_CODES=*** borgnoexist --version --debug --show-rc /root/tmp/test.yaml: /root/tmp/test.yaml: Error getting local Borg version /root/tmp/test.yaml: [Errno 2] No such file or directory: 'borgnoexist' /root/tmp/test.yaml: Running after configuration command hook /root/tmp/test.yaml: echo '*** start services' *** start services /root/tmp/test.yaml: Running after configuration command hook /root/tmp/test.yaml: echo '*** service remove backup' *** service remove backup /root/tmp/test.yaml: An error occurred summary: /root/tmp/test.yaml: Loading configuration file An error occurred /root/tmp/test.yaml: Error getting local Borg version [Errno 2] No such file or directory: 'borgnoexist' Need some help? https://torsion.org/borgmatic/#issues ``` ### Expected behavior Any `finish` state command hooks should not run when an error occurs. ### Other notes / implementation ideas Fortunately, this bug appears to be restricted to the specific case of `borg --version` failing. That's because when such an error occurs in `run_configuration()`, there's a `return` instead of a `raise`. And therefore the state gets considered a `finish` instead of a `fail`. The fix is probably to change that `return` to a `raise`. ### borgmatic version main ### borgmatic installation method pipx install --editable ### Borg version 1.4.1 ### Python version 3.13.3 ### Database version (if applicable) _No response_ ### Operating system and version Manjaro stable
Author
Owner

Fixed in main and will be part of the next release.

Fixed in main and will be part of the next release.
Author
Owner

Released in borgmatic 2.0.7!

Released in borgmatic 2.0.7!
Sign in to join this conversation.
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
borgmatic-collective/borgmatic#1100
No description provided.