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

Always terminate the container if pihole-FTL binary exits #1649

Merged
merged 4 commits into from
Oct 10, 2024

Conversation

PromoFaux
Copy link
Member

What does this PR aim to accomplish?:

Inspired by a conversation with @DL6ER - currently if pihole-FTL crashes, the container just sits there doing nothing - users are unable to do anything with it.

This PR addresses the issue by ensure that the container is always terminated in case of pihole-FTL being terminated. We also store the exit code of FTL, so that we can exit the container with the same code (useful in case of on-failure[:max-retries] restart policy)

`docker stop pihole`
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: ########## FTL started on 6ccaf1acbf31! ##########
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: FTL branch: development
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: FTL version: vDev-e558f79
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: FTL commit: e558f798
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: FTL date: 2024-10-05 18:06:26 +0200
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: FTL user: pihole
pihole  | 2024-10-06 22:41:12.532 BST [56M] INFO: Compiled for linux/amd64 (compiled on CI) using cc (Alpine 13.2.1_git20240309) 13.2.1 20240309
pihole  | 2024-10-06 22:41:12.533 BST [56M] INFO: Wrote config file:
pihole  | 2024-10-06 22:41:12.533 BST [56M] INFO:  - 150 total entries
pihole  | 2024-10-06 22:41:12.533 BST [56M] INFO:  - 147 entries are default
pihole  | 2024-10-06 22:41:12.533 BST [56M] INFO:  - 3 entries are modified
pihole  | 2024-10-06 22:41:12.533 BST [56M] INFO:  - 0 entries are forced through environment
pihole  | 2024-10-06 22:41:12.534 BST [56M] INFO: Parsed config file /etc/pihole/pihole.toml successfully
pihole  | 2024-10-06 22:41:12.534 BST [56M] WARNING: Insufficient permissions to set process priority to -10 (CAP_SYS_NICE required), process priority remains at 0
pihole  | 2024-10-06 22:41:12.535 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:41:12.535 BST [56M] INFO: listening on 0.0.0.0 port 53
pihole  | 2024-10-06 22:41:12.535 BST [56M] INFO: listening on :: port 53
pihole  | 2024-10-06 22:41:12.535 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:41:12.536 BST [56M] INFO: Database version is 19
pihole  | 2024-10-06 22:41:12.536 BST [56M] INFO: Database successfully initialized
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO: Imported 0 queries from the on-disk database (it has 0 rows)
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO: Parsing queries in database
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO: Imported 0 queries from the long-term database
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Total DNS queries: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Cached DNS queries: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Forwarded DNS queries: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Blocked DNS queries: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Unknown DNS queries: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Unique domains: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Unique clients: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> DNS cache records: 0
pihole  | 2024-10-06 22:41:12.537 BST [56M] INFO:  -> Known forward destinations: 0
pihole  | 2024-10-06 22:41:12.553 BST [56M] WARNING: Insufficient permissions to set system time (CAP_SYS_TIME required), NTP client not available
pihole  | 2024-10-06 22:41:12.553 BST [56/T58] INFO: NTP server listening on :::123 (IPv6)
pihole  | 2024-10-06 22:41:12.553 BST [56/T57] INFO: NTP server listening on 0.0.0.0:123 (IPv4)
pihole  | 2024-10-06 22:41:12.553 BST [56M] INFO: FTL is running as user pihole (UID 1000)
pihole  | 2024-10-06 22:41:12.553 BST [56M] INFO: Reading certificate from /etc/pihole/tls.pem ...
pihole  | 2024-10-06 22:41:12.554 BST [56M] INFO: Using SSL/TLS certificate file /etc/pihole/tls.pem
pihole  | 2024-10-06 22:41:12.554 BST [56M] INFO: Restored 0 API sessions from the database
pihole  | 2024-10-06 22:41:12.555 BST [56M] INFO: Blocking status is enabled
pihole  | 2024-10-06 22:41:12.654 BST [56/T59] INFO: Compiled 0 allow and 0 deny regex for 0 client in 0.0 msec
pihole  | 
pihole  |   [i] Container stop requested...
pihole  |   [i] pihole-FTL is running - Attempting to shut it down cleanly
pihole  | 
pihole  | 2024-10-06 22:41:16.699 BST [56M] INFO: Asked to terminate by "N/A" (PID 171, user root UID 0)
pihole  | 2024-10-06 22:41:16.759 BST [56/T62] INFO: Terminating timer thread
pihole  | 2024-10-06 22:41:16.762 BST [56/T59] INFO: Terminating database thread
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: Finished final database update
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: Waiting for threads to join
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: Thread housekeeper (1) is idle, terminating it.
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: Thread dns-client (2) is idle, terminating it.
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: All threads joined
pihole  | 2024-10-06 22:41:16.950 BST [56M] INFO: Stored 0 API sessions in the database
pihole  | 2024-10-06 22:41:17.564 BST [56M] INFO: ########## FTL terminated after 5s 31ms  (code 0)! ##########
pihole  | 
pihole  |   [i] pihole-FTL exited with status 0
pihole  | 
pihole  |   [i] Container will now stop or restart depending on your restart policy
pihole  |       https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy
pihole  | 
pihole exited with code 0
`docker exec -it pihole killall --signal 15 pihole-FTL`
pihole  | 2024-10-06 22:42:42.417 BST [56M] INFO: ########## FTL started on 0d9149f7f785! ##########
pihole  | 2024-10-06 22:42:42.417 BST [56M] INFO: FTL branch: development
pihole  | 2024-10-06 22:42:42.417 BST [56M] INFO: FTL version: vDev-e558f79
pihole  | 2024-10-06 22:42:42.417 BST [56M] INFO: FTL commit: e558f798
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO: FTL date: 2024-10-05 18:06:26 +0200
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO: FTL user: pihole
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO: Compiled for linux/amd64 (compiled on CI) using cc (Alpine 13.2.1_git20240309) 13.2.1 20240309
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO: Wrote config file:
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO:  - 150 total entries
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO:  - 147 entries are default
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO:  - 3 entries are modified
pihole  | 2024-10-06 22:42:42.418 BST [56M] INFO:  - 0 entries are forced through environment
pihole  | 2024-10-06 22:42:42.419 BST [56M] INFO: Parsed config file /etc/pihole/pihole.toml successfully
pihole  | 2024-10-06 22:42:42.419 BST [56M] WARNING: Insufficient permissions to set process priority to -10 (CAP_SYS_NICE required), process priority remains at 0
pihole  | 2024-10-06 22:42:42.419 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:42:42.419 BST [56M] INFO: listening on 0.0.0.0 port 53
pihole  | 2024-10-06 22:42:42.419 BST [56M] INFO: listening on :: port 53
pihole  | 2024-10-06 22:42:42.420 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:42:42.420 BST [56M] INFO: Database version is 19
pihole  | 2024-10-06 22:42:42.420 BST [56M] INFO: Database successfully initialized
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO: Imported 0 queries from the on-disk database (it has 0 rows)
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO: Parsing queries in database
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO: Imported 0 queries from the long-term database
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Total DNS queries: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Cached DNS queries: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Forwarded DNS queries: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Blocked DNS queries: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Unknown DNS queries: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Unique domains: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Unique clients: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> DNS cache records: 0
pihole  | 2024-10-06 22:42:42.421 BST [56M] INFO:  -> Known forward destinations: 0
pihole  | 2024-10-06 22:42:42.436 BST [56M] WARNING: Insufficient permissions to set system time (CAP_SYS_TIME required), NTP client not available
pihole  | 2024-10-06 22:42:42.436 BST [56/T57] INFO: NTP server listening on 0.0.0.0:123 (IPv4)
pihole  | 2024-10-06 22:42:42.436 BST [56/T58] INFO: NTP server listening on :::123 (IPv6)
pihole  | 2024-10-06 22:42:42.436 BST [56M] INFO: FTL is running as user pihole (UID 1000)
pihole  | 2024-10-06 22:42:42.436 BST [56M] INFO: Reading certificate from /etc/pihole/tls.pem ...
pihole  | 2024-10-06 22:42:42.437 BST [56M] INFO: Using SSL/TLS certificate file /etc/pihole/tls.pem
pihole  | 2024-10-06 22:42:42.437 BST [56M] INFO: Restored 0 API sessions from the database
pihole  | 2024-10-06 22:42:42.438 BST [56M] INFO: Blocking status is enabled
pihole  | 2024-10-06 22:42:42.537 BST [56/T59] INFO: Compiled 0 allow and 0 deny regex for 0 client in 0.0 msec
pihole  | 2024-10-06 22:42:45.519 BST [56M] INFO: Asked to terminate by "N/A" (PID 168, user root UID 0)
pihole  | 2024-10-06 22:42:45.541 BST [56/T62] INFO: Terminating timer thread
pihole  | 2024-10-06 22:42:45.542 BST [56/T59] INFO: Terminating database thread
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: Finished final database update
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: Waiting for threads to join
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: Thread housekeeper (1) is idle, terminating it.
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: Thread dns-client (2) is idle, terminating it.
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: All threads joined
pihole  | 2024-10-06 22:42:45.770 BST [56M] INFO: Stored 0 API sessions in the database
pihole  | 2024-10-06 22:42:46.439 BST [56M] INFO: ########## FTL terminated after 4s 21ms  (code 0)! ##########
pihole  | 
pihole  |   [i] pihole-FTL exited with status 0
pihole  | 
pihole  |   [i] Container will now stop or restart depending on your restart policy
pihole  |       https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy
pihole  | 
pihole exited with code 0
`docker exec -it pihole killall --signal 11 pihole-FTL`
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: ########## FTL started on 6178e7ef0dd1! ##########
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: FTL branch: development
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: FTL version: vDev-e558f79
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: FTL commit: e558f798
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: FTL date: 2024-10-05 18:06:26 +0200
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: FTL user: pihole
pihole  | 2024-10-06 22:43:19.256 BST [56M] INFO: Compiled for linux/amd64 (compiled on CI) using cc (Alpine 13.2.1_git20240309) 13.2.1 20240309
pihole  | 2024-10-06 22:43:19.257 BST [56M] INFO: Wrote config file:
pihole  | 2024-10-06 22:43:19.257 BST [56M] INFO:  - 150 total entries
pihole  | 2024-10-06 22:43:19.257 BST [56M] INFO:  - 147 entries are default
pihole  | 2024-10-06 22:43:19.257 BST [56M] INFO:  - 3 entries are modified
pihole  | 2024-10-06 22:43:19.257 BST [56M] INFO:  - 0 entries are forced through environment
pihole  | 2024-10-06 22:43:19.258 BST [56M] INFO: Parsed config file /etc/pihole/pihole.toml successfully
pihole  | 2024-10-06 22:43:19.258 BST [56M] WARNING: Insufficient permissions to set process priority to -10 (CAP_SYS_NICE required), process priority remains at 0
pihole  | 2024-10-06 22:43:19.258 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:43:19.258 BST [56M] INFO: listening on 0.0.0.0 port 53
pihole  | 2024-10-06 22:43:19.258 BST [56M] INFO: listening on :: port 53
pihole  | 2024-10-06 22:43:19.259 BST [56M] INFO: PID of FTL process: 56
pihole  | 2024-10-06 22:43:19.259 BST [56M] INFO: Database version is 19
pihole  | 2024-10-06 22:43:19.259 BST [56M] INFO: Database successfully initialized
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO: Imported 0 queries from the on-disk database (it has 0 rows)
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO: Parsing queries in database
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO: Imported 0 queries from the long-term database
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Total DNS queries: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Cached DNS queries: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Forwarded DNS queries: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Blocked DNS queries: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Unknown DNS queries: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Unique domains: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Unique clients: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> DNS cache records: 0
pihole  | 2024-10-06 22:43:19.260 BST [56M] INFO:  -> Known forward destinations: 0
pihole  | 2024-10-06 22:43:19.276 BST [56M] WARNING: Insufficient permissions to set system time (CAP_SYS_TIME required), NTP client not available
pihole  | 2024-10-06 22:43:19.276 BST [56/T57] INFO: NTP server listening on 0.0.0.0:123 (IPv4)
pihole  | 2024-10-06 22:43:19.276 BST [56/T58] INFO: NTP server listening on :::123 (IPv6)
pihole  | 2024-10-06 22:43:19.276 BST [56M] INFO: FTL is running as user pihole (UID 1000)
pihole  | 2024-10-06 22:43:19.276 BST [56M] INFO: Reading certificate from /etc/pihole/tls.pem ...
pihole  | 2024-10-06 22:43:19.276 BST [56M] INFO: Using SSL/TLS certificate file /etc/pihole/tls.pem
pihole  | 2024-10-06 22:43:19.277 BST [56M] INFO: Restored 0 API sessions from the database
pihole  | 2024-10-06 22:43:19.278 BST [56M] INFO: Blocking status is enabled
pihole  | 2024-10-06 22:43:19.377 BST [56/T59] INFO: Compiled 0 allow and 0 deny regex for 0 client in 0.0 msec
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: ---------------------------->  FTL crashed!  <----------------------------
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: Please report a bug at https://github.com/pi-hole/FTL/issues
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: and include in your report already the following details:
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL has been running for 8 seconds
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL branch: development
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL version: vDev-e558f79
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL commit: e558f798
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL date: 2024-10-05 18:06:26 +0200
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: FTL user: started as pihole, ended as pihole
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: Compiled for linux/amd64 (compiled on CI) using cc (Alpine 13.2.1_git20240309) 13.2.1 20240309
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: Process details: MID: 56
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:                  PID: 56
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:                  TID: 56
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:                  Name: pihole-FTL
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: Received signal: Segmentation fault
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:      at address: 0xa8
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:      with code:  Unknown (0)
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: !!! INFO: pihole-FTL has not been compiled with glibc/backtrace support, not generating one !!!
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: ------ Listing content of directory /dev/shm ------
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: File Mode User:Group      Size  Filename
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rwxrwxrwx root:root       280   .
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rwxr-xr-x root:root       340   ..
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole   569K  FTL-fifo-log
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole     4K  FTL-per-client-regex
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole    20K  FTL-dns-cache
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole     8K  FTL-overTime
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole   295K  FTL-queries
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole    29K  FTL-upstreams
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole    86K  FTL-clients
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole     4K  FTL-domains
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole    82K  FTL-strings
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole   136   FTL-settings
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole   304   FTL-counters
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: rw------- pihole:pihole    88   FTL-lock
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: ---------------------------------------------------
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Please also include some lines from above the !!!!!!!!! header.
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Thank you for helping us to improve our FTL engine!
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Waiting for threads to join
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Thread database (0) is idle, terminating it.
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Thread housekeeper (1) is idle, terminating it.
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Thread dns-client (2) is idle, terminating it.
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Thread timer (3) is idle, terminating it.
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: All threads joined
pihole  | 2024-10-06 22:43:27.355 BST [56M] INFO: Stored 0 API sessions in the database
pihole  | 2024-10-06 22:43:28.285 BST [56M] INFO: ########## FTL terminated after 9s 29ms  (code 1)! ##########
pihole  | 
pihole  |   [i] pihole-FTL exited with status 1
pihole  | 
pihole  |   [i] Container will now stop or restart depending on your restart policy
pihole  |       https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy
pihole  | 
pihole exited with code 1

By submitting this pull request, I confirm the following:

  1. I have read and understood the contributors guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented my proposed changes within the code and I have tested my changes.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the EUPL 1.2 license
  5. I have squashed any insignificant commits. (git rebase)

  • I have read the above and my PR is ready for review. Check this box to confirm

@PromoFaux PromoFaux requested review from DL6ER and a team October 6, 2024 21:47
…urally or via an error. Don't attempt to restart it, allow the container's restart policy to do this.

Signed-off-by: Adam Warner <[email protected]>
@yubiuser
Copy link
Member

yubiuser commented Oct 6, 2024

Are you trying to re-invent what s6-overlay did? Maybe we should consider to add it again.

@PromoFaux
Copy link
Member Author

Not hugely - S6 is heavier than it needs to be for what we're doing. As this stands, its what the container should have been doing from the start. If FTL is not running, then the container should also stop.

@rdwebdesign

This comment was marked as resolved.

@PromoFaux

This comment was marked as resolved.

@PromoFaux

This comment was marked as resolved.

@rdwebdesign

This comment was marked as resolved.

@DL6ER
Copy link
Member

DL6ER commented Oct 7, 2024

How much overhead is there when starting the container? I think I've seen in some thread (sorry, don't have a precise reference right now) a mentioning that restarting the container is slow on low-end hardware because of something else (gravity?) happening before the container comes to the point where FTL is launched.

Edit: Reference here: pi-hole/FTL#2077

I need to restart pi-hole and wait for database rebuilds before DNS resolutions work again, which can take upwards of 20 minutes.

Note this is a v5 deployment, v6 may already have improved on this.

@PromoFaux
Copy link
Member Author

That depends entirely.

This function is run on every container start before FTL is started:

migrate_gravity() {
echo " [i] Gravity migration checks"
gravityDBfile=$(getFTLConfigValue files.gravity)
if [[ -z "${PYTEST}" ]]; then
if [[ ! -f /etc/pihole/adlists.list ]]; then
echo " [i] No adlist file found, creating one with a default blocklist"
echo "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" >/etc/pihole/adlists.list
fi
fi
if [ ! -f "${gravityDBfile}" ]; then
echo " [i] ${gravityDBfile} does not exist (Likely due to a fresh volume). This is a required file for Pi-hole to operate."
echo " [i] Gravity will now be run to create the database"
pihole -g
else
echo " [i] Existing gravity database found"
# source the migration script and run the upgrade function
source /etc/.pihole/advanced/Scripts/database_migration/gravity-db.sh
upgrade_gravityDB "${gravityDBfile}" "/etc/pihole"
fi
echo ""
}

If it is a new container, and gravity.db does not exist, then pihole -g will be run to generate the database, however on all subsequent startups - only the gravity upgrade scripts are run. Otherwise gravity will run once a week as normal.

Note this is a v5 deployment, v6 may already have improved on this.

In the v5 image, gravity runs on every container start although there is an environment variable there to disable that behaviour

image

Note that this environment variable does nothing on V6, as we have removed the gravity on boot entirely (unless the database does not exist)

@yubiuser
Copy link
Member

yubiuser commented Oct 7, 2024

Not hugely - S6 is heavier than it needs to be for what we're doing.

I still think the hand-crafted solution this PR adds is more complicated and error-prone than using s6. The issue I see here is that we are starting FTL from within a bash script that is sourced by another bash script, running it in the background, moving the exit code around and kill one bash script in case of failure from within another. The logic is difficult to follow.

s6 adds ~5,2 MB when added via a multi-stage build. We could easily add different "pre-start" scripts e.g. to migrate v5-> v6 (and maybe later v6 -> v7), could use s6's notifycheck to signal FTL is ready and handle all the shudown in a clean way.

<missing>      8 days ago    COPY init /init # buildkit                      1.01kB    buildkit.dockerfile.v0
<missing>      8 days ago    COPY /etc/s6-overlay/ /etc/s6-overlay/ # bui…   14B       buildkit.dockerfile.v0
<missing>      8 days ago    COPY /package/ /package/ # buildkit             5.11MB    buildkit.dockerfile.v0
<missing>      8 days ago    COPY /command /command/ # buildkit              10.8kB    buildkit.dockerfile.v0

…start_ftl process and pass it's exit code to stop.

Add in additional while/wait to ensure that after the FTL log exists, it contains the "FTL Started" line

Signed-off-by: Adam Warner <[email protected]>
@PromoFaux
Copy link
Member Author

I'm still not a fan of adding s6 - removing it was one of the motivating factors for the v6 rewrite.

I've made some adjustments based on your comments, and it should be much easier to follow now

src/start.sh Outdated Show resolved Hide resolved
Co-authored-by: RD WebDesign <[email protected]>
Signed-off-by: Adam Warner <[email protected]>
rdwebdesign
rdwebdesign previously approved these changes Oct 7, 2024
Copy link
Member

@yubiuser yubiuser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shut it down with docker stop pihole and the exit code reported is not the one from FTL, but from the container istself

2024-10-08 19:40:29.879 UTC [164M] INFO: Thread housekeeper (1) is idle, terminating it.
2024-10-08 19:40:29.879 UTC [164M] INFO: All threads joined
2024-10-08 19:40:29.880 UTC [164M] INFO: Stored 0 API sessions in the database
2024-10-08 19:40:30.630 UTC [164M] INFO: ########## FTL terminated after 9s 122ms  (code 0)! ##########

  [i] pihole-FTL exited with status 143

Is it expected that FTL crashes when signal 11 is sent?

@rdwebdesign
Copy link
Member

Apparently docker stop will send signal 15:

2024-10-08 16:46:50.802 -03 [26140M] INFO: Asked to terminate by "killall --signal 15 pihole-FTL" (PID 26161, user root UID 0)

@PromoFaux
Copy link
Member Author

PromoFaux commented Oct 8, 2024

[i] pihole-FTL exited with status 143

Yeah, I saw this, this is actually SIGTERM, and signals that the container gracefully stopped. What I am not sure about here, though, is why it's getting populated in the exit code of FTL

Signal 11 is what @DL6ER told me to use in order to simulate a crash

@PromoFaux
Copy link
Member Author

Log after a docker compose stop:

pihole  | 2024-10-08 16:43:10.653 BST [50/T88] INFO: Compiled 0 allow and 0 deny regex for 0 client in 0.0 msec
pihole  | 
pihole  |   [i] Container stop requested...
pihole  |   [i] pihole-FTL is running - Attempting to shut it down cleanly
pihole  | 
pihole  | 2024-10-08 20:51:25.081 BST [50M] INFO: Asked to terminate by "N/A" (PID 5641, user root UID 0)
pihole  | 2024-10-08 20:51:25.104 BST [50/T88] INFO: Terminating database thread
pihole  | 2024-10-08 20:51:25.172 BST [50/T91] INFO: Terminating timer thread
pihole  | 2024-10-08 20:51:25.331 BST [50M] INFO: Finished final database update
pihole  | 2024-10-08 20:51:25.332 BST [50M] INFO: Waiting for threads to join
pihole  | 2024-10-08 20:51:25.332 BST [50M] INFO: Thread housekeeper (1) is idle, terminating it.
pihole  | 2024-10-08 20:51:25.332 BST [50M] INFO: Thread dns-client (2) is idle, terminating it.
pihole  | 2024-10-08 20:51:25.332 BST [50M] INFO: All threads joined
pihole  | 2024-10-08 20:51:25.340 BST [50M] INFO: Stored 0 API sessions in the database
pihole  | 2024-10-08 20:51:25.601 BST [50M] INFO: ########## FTL terminated after 4h 8m 15s  (code 0)! ##########
pihole  | 
pihole  |   [i] pihole-FTL exited with status 143

@PromoFaux
Copy link
Member Author

Apparently docker stop will send signal 15:

Yes

trap stop TERM INT QUIT HUP ERR
[snip]
stop() {
  local FTL_EXIT_CODE=$1

  # Only attempt to close pihole-FTL if it is running, it may already have crashed
  if pgrep pihole-FTL >/dev/null; then
    echo ""
    echo "  [i] Container stop requested..."
    echo "  [i] pihole-FTL is running - Attempting to shut it down cleanly"
    echo ""
    # Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
    ftl_pid=$(pgrep pihole-FTL)

    killall --signal 15 pihole-FTL
[snip]

@yubiuser
Copy link
Member

yubiuser commented Oct 8, 2024

Maybe it is due to the return in the trap

https://unix.stackexchange.com/a/667384

@PromoFaux
Copy link
Member Author

@yubiuser try this

@DL6ER
Copy link
Member

DL6ER commented Oct 9, 2024

Signal 11 is what @DL6ER told me to use in order to simulate a crash

On a crash, this is the signal that is sent by the kernel to the process to allow for a (semi-)graceful shutdown. Sending it manually to the process is entirely artificial in this case but indeed a rather often used mechanism to simulate a crash. FTL will be unable to say where the crash happened in this case as seen in

pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:                  Name: pihole-FTL
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO: Received signal: Segmentation fault
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:      at address: 0xa8
pihole  | 2024-10-06 22:43:27.354 BST [56M] INFO:      with code:  Unknown (0)

(copied from the PR description text)

DL6ER
DL6ER previously requested changes Oct 9, 2024
Copy link
Member

@DL6ER DL6ER left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See individual comments.

Comment on lines +79 to +87
# Wait until the FTL log contains the "FTL started" message before continuing
while ! grep -q '########## FTL started' /var/log/pihole/FTL.log; do
sleep 0.5
done
Copy link
Member

@DL6ER DL6ER Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What when the container is (re)started more than once per day? In this case, you may have this message multiple times in the file. I don't think restarting the container on the same day is unlikely, especially when users are playing around with environment variables, etc.

Obviously my comment assumes you have persistent /var/log but this seems like a sensible thing users may be doing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again at:

pihole  | 2024-10-06 22:42:42.417 BST [56M] INFO: ########## FTL started on 0d9149f7f785! ##########

A working solution may be reading the PID from the PID file and expecting this in the log, like

Suggested change
# Wait until the FTL log contains the "FTL started" message before continuing
while ! grep -q '########## FTL started' /var/log/pihole/FTL.log; do
sleep 0.5
done
# Wait until the FTL log contains the "FTL started" message before continuing
FTL_PID_FILE="$(getFTLConfigValue files.pid)"
FTL_PID="$(cat "${FTL_PID_FILE}")"
while ! grep -q "[${FTL_PID}M] INFO: ########## FTL started" /var/log/pihole/FTL.log; do
sleep 0.5
done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried a few times to get this grep working (I had to put the FTL_PID= part into a while loop until it was populated otherwise FTL_PID was blank - however the grep didn't work, and I don't really know why!

src/start.sh Show resolved Hide resolved
@@ -75,33 +97,63 @@ start() {
# Start tailing the FTL log from the most recent "FTL Started" message
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Start tailing the FTL log from the most recent "FTL Started" message

This existing comment (emphasis on "the most recent") seems to support my comment above concerning the non-uniqueness of this message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it depends on the machine (just from looking at logs posted by others here) but the FTL pid is almost always the same between container restarts/destructions - so it doesn't really make for much of a unique identifier

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although now in three consecutive runs on my machine, this does not ring true. 55, then 54, then 53

src/start.sh Outdated Show resolved Hide resolved
src/start.sh Outdated Show resolved Hide resolved
src/start.sh Outdated Show resolved Hide resolved
@PromoFaux PromoFaux force-pushed the terminate-with-FTL branch 2 times, most recently from e640e08 to 931b216 Compare October 9, 2024 18:09
echo ""
echo " [i] Container stop requested..."
echo " [i] pihole-FTL is running - Attempting to shut it down cleanly"
echo ""
# Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
ftl_pid=$(pgrep pihole-FTL)

killall --signal 15 pihole-FTL
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it's worth doing something like:

(sleep 3 && killall --signal 15 pihole-FTL) &
wait $CAPSH_PID

in case the $CAPSH_PID is already dead by the time we get to it after killing FTL? Or even

killall --signal 15 pihole-FTL &
wait $CAPSH_PID

Am I just being overly paranoid about slower systems here?

@yubiuser
Copy link
Member

yubiuser commented Oct 9, 2024

Last commit fixed the propagation and stop via docker stop pihole. But "crashing" (--singal 11) does not work anymore.

2024-10-09 19:14:16.782 UTC [164M] INFO: Stored 0 API sessions in the database
2024-10-09 19:14:17.492 UTC [164M] INFO: ########## FTL terminated after 31s  (code 1)! ##########

  [i] pihole-FTL exited with status 0

@PromoFaux
Copy link
Member Author

Huh, could have sworn it did when I tried it... I'll go back and try again!

… code being passed to it, then FTL is still running - so we stop FTL and then get the exit code from the capsh PID by immediately `wait`ing it after killing FTL

Signed-off-by: Adam Warner <[email protected]>
@PromoFaux PromoFaux merged commit d762e91 into development Oct 10, 2024
6 checks passed
@PromoFaux PromoFaux deleted the terminate-with-FTL branch October 10, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants