-
Notifications
You must be signed in to change notification settings - Fork 31
Avoid deadlocks after fork #303
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
Open
danth
wants to merge
5
commits into
eclipse-score:main
Choose a base branch
from
etas-contrib:avoid-deadlocks-after-fork
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
40a2b95
Add missing `sysexit` in `handleComms`
danth d539804
Implement async signal safe logging utility
danth 3b0ea35
Replace log calls with signal safe version after forking
danth 991ccd9
Add comments about signal safety
danth c8e1a91
Simplifying using sfinae
MaciejKaszynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
score/launch_manager/src/daemon/src/common/signal_safe_log.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include <charconv> | ||
| #include <unistd.h> | ||
| #include <array> | ||
| #include <cstdlib> | ||
| #include <string_view> | ||
|
|
||
| #ifndef SIGNAL_SAFE_LOG_HPP_INCLUDED | ||
| #define SIGNAL_SAFE_LOG_HPP_INCLUDED | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| /// @brief Utility for building log messages in an async signal safe context. | ||
| template <int C> | ||
| class signal_safe_buffer | ||
| { | ||
| public: | ||
| std::array<char, C> buffer = {}; | ||
| size_t start = 0; | ||
| size_t end = 0; | ||
|
|
||
| /// @brief Append the given string to the buffer. | ||
| template <typename T, std::enable_if_t<std::is_convertible_v<T, std::string_view>, bool> = true> | ||
| void append(const T& value) | ||
| { | ||
| const auto string = std::string_view(value); | ||
|
|
||
| size_t length = std::min(string.length(), buffer.size() - end); | ||
|
|
||
| for (size_t index = 0; index < length; ++index) | ||
| { | ||
| buffer[end++] = string[index]; | ||
| } | ||
| } | ||
|
|
||
| /// @brief Append the given integer to the buffer. | ||
| template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true> | ||
| void append(const T& value) | ||
| { | ||
| const auto [pointer, error] = std::to_chars(&buffer[end], &buffer.back(), value); | ||
|
|
||
| if (error == std::errc{}) | ||
| { | ||
| end = pointer - &buffer.front(); | ||
| } | ||
| } | ||
|
|
||
| /// @brief Write out the buffer to standard error. | ||
| /// @return Whether the write succeeded. True means the whole message was | ||
| /// written. False means nothing, or only part was written. | ||
| [[nodiscard]] bool flush() | ||
| { | ||
| while (start < end) | ||
| { | ||
| const auto written = write(STDERR_FILENO, &buffer[start], end - start); | ||
|
|
||
| if (written >= 0) | ||
| { | ||
| start += written; | ||
| } | ||
| else if (errno != EINTR) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Rewind the indices in case the buffer is used again. | ||
| start = 0; | ||
| end = 0; | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace score::lcm::internal | ||
| { | ||
|
|
||
| template <typename... T> | ||
| [[nodiscard]] bool signal_safe_log(const T&... values) | ||
| { | ||
| const int original_errno = errno; // For reentrancy | ||
|
|
||
| signal_safe_buffer<1024> buffer = {}; | ||
| (buffer.append(values), ...); | ||
| buffer.append("\n"); | ||
| const bool written = buffer.flush(); | ||
|
|
||
| errno = original_errno; // For reentrancy | ||
|
|
||
| return written; | ||
| } | ||
|
|
||
| template <typename... T> | ||
| [[nodiscard]] bool signal_safe_log_errno(int log_errno, const T&... values) | ||
| { | ||
| #if _GNU_SOURCE && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 32 | ||
| // strerrordesc_np is documented as async signal safe. | ||
| return signal_safe_log(values..., " (", strerrordesc_np(log_errno), ")"); | ||
| #elif __QNXNTO__ | ||
| // QNX strerror is documented as async signal safe. | ||
| return signal_safe_log(values..., " (", strerror(log_errno), ")"); | ||
| #else | ||
| // POSIX strerror is not async signal safe. Log the raw number instead. | ||
| return signal_safe_log(values..., " (errno ", log_errno, ")"); | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace score::lcm::internal | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the conclusion on using std::to_chars in a signal-safe context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://gcc.gnu.org/onlinedocs/gcc-13.1.0/libstdc++/api/a00062_source.html (line 130)
It only accesses local variables and the buffer it was given. I don't see anything which stands out to me as unsafe