-
Notifications
You must be signed in to change notification settings - Fork 279
protect access to global logging calls with a mutex #562
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,72 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "./execute_with_logging_mutex.h" // NOLINT(build/include) | ||
|
|
||
| // Must be before Python.h; makes #-formats use ssize_t instead of int | ||
| #define PY_SSIZE_T_CLEAN | ||
| #include <Python.h> | ||
|
|
||
| #include "rcutils/logging.h" | ||
| #include "rcutils/visibility_control_macros.h" | ||
|
|
||
| #include "./logging_mutex.hpp" | ||
|
|
||
| extern "C" | ||
| { | ||
| PyObject * | ||
| rclpy_detail_execute_with_logging_mutex( | ||
| rclpy_detail_execute_with_logging_mutex_sig function, | ||
| PyObject * self, | ||
| PyObject * args) | ||
| { | ||
| try { | ||
| std::lock_guard<std::recursive_mutex> guard(rclpy::detail::get_global_logging_mutex()); | ||
| return function(self, args); | ||
| } catch (std::exception & ex) { | ||
| PyErr_Format( | ||
| PyExc_RuntimeError, | ||
| "Failed to acquire logging mutex: %s", ex.what()); | ||
| return NULL; | ||
| } catch (...) { | ||
| PyErr_Format( | ||
| PyExc_RuntimeError, | ||
| "Failed to acquire logging mutex"); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| void | ||
| rclpy_detail_execute_with_logging_mutex2( | ||
| rclpy_detail_execute_with_logging_mutex_sig2 function, | ||
| void * arg) | ||
| { | ||
| try { | ||
| std::lock_guard<std::recursive_mutex> guard(rclpy::detail::get_global_logging_mutex()); | ||
| function(arg); | ||
| } catch (std::exception & ex) { | ||
| // Warning should use line number of the current stack frame | ||
| int stack_level = 1; | ||
| PyErr_WarnFormat( | ||
| PyExc_RuntimeWarning, stack_level, | ||
| "Failed to acquire logging mutex: %s", ex.what()); | ||
| } catch (...) { | ||
| // Warning should use line number of the current stack frame | ||
| int stack_level = 1; | ||
| PyErr_WarnFormat( | ||
| PyExc_RuntimeWarning, stack_level, | ||
| "Failed to acquire logging mutex"); | ||
| } | ||
| } | ||
| } // extern "C" |
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,56 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef RCLPY__DETAIL__EXECUTE_WITH_LOGGING_MUTEX_H_ | ||
| #define RCLPY__DETAIL__EXECUTE_WITH_LOGGING_MUTEX_H_ | ||
|
|
||
| // Must be before Python.h; makes #-formats use ssize_t instead of int | ||
| #define PY_SSIZE_T_CLEAN | ||
| #include <Python.h> | ||
|
|
||
| #include "rcutils/logging.h" | ||
| #include "rcutils/visibility_control_macros.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" | ||
| { | ||
| #endif | ||
|
|
||
| typedef PyObject * (* rclpy_detail_execute_with_logging_mutex_sig)(PyObject *, PyObject *); | ||
|
|
||
| /// Execute the given function pointer after acquiring the logging mutex. | ||
| RCUTILS_LOCAL | ||
| PyObject * | ||
| rclpy_detail_execute_with_logging_mutex( | ||
| rclpy_detail_execute_with_logging_mutex_sig function, | ||
| PyObject * self, | ||
| PyObject * args); | ||
|
|
||
| typedef void (* rclpy_detail_execute_with_logging_mutex_sig2)(void *); | ||
|
|
||
| /// Execute the given function pointer after acquiring the logging mutex. | ||
| /** | ||
| * This version has a slightly different signature which is used when destroying a node. | ||
| */ | ||
| RCUTILS_LOCAL | ||
| void | ||
| rclpy_detail_execute_with_logging_mutex2( | ||
| rclpy_detail_execute_with_logging_mutex_sig2 function, | ||
| void * arg); | ||
|
|
||
| #ifdef __cplusplus | ||
| } // extern "C" | ||
| #endif | ||
|
|
||
| #endif // RCLPY__DETAIL__EXECUTE_WITH_LOGGING_MUTEX_H_ |
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,27 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "./logging_mutex.hpp" | ||
|
|
||
| #include <memory> | ||
| #include <mutex> | ||
|
|
||
| #include "rcutils/macros.h" | ||
|
|
||
| std::recursive_mutex & | ||
| rclpy::detail::get_global_logging_mutex() | ||
| { | ||
| static std::recursive_mutex mutex; | ||
| return mutex; | ||
| } |
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,47 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #ifndef RCLPY__DETAIL__LOGGING_MUTEX_HPP_ | ||
| #define RCLPY__DETAIL__LOGGING_MUTEX_HPP_ | ||
|
|
||
| #include <memory> | ||
| #include <mutex> | ||
|
|
||
| #include "rcutils/visibility_control_macros.h" | ||
|
|
||
| namespace rclpy | ||
| { | ||
| namespace detail | ||
| { | ||
|
|
||
| /// Global logging mutex | ||
| /** | ||
| * This mutex is locked in the following situations: | ||
| * - In initialization/destruction of contexts. | ||
| * - In initialization/destruction of nodes. | ||
| * - In the rcl logging output handler installed by rclpy, | ||
| * i.e.: in all calls to the logger macros, including RCUTILS_* ones. | ||
| */ | ||
| // Implementation detail: | ||
| // A shared pointer to the mutex is used, so that objects that need to use | ||
| // it at destruction time can keep it alive. | ||
| // In that way, a destruction ordering problem between static objects is avoided. | ||
| RCUTILS_LOCAL | ||
| std::recursive_mutex & | ||
| get_global_logging_mutex(); | ||
|
|
||
| } // namespace detail | ||
| } // namespace rclpy | ||
|
|
||
| #endif // RCLPY__DETAIL__LOGGING_MUTEX_HPP_ | ||
43 changes: 43 additions & 0 deletions
43
rclpy/src/rclpy/detail/thread_safe_logging_output_handler.cpp
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,43 @@ | ||
| // Copyright 2020 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "./thread_safe_logging_output_handler.h" // NOLINT(build/include) | ||
|
|
||
| #include "rcl/logging.h" | ||
|
|
||
| #include "./logging_mutex.hpp" | ||
|
|
||
| extern "C" | ||
| { | ||
| void | ||
| rclpy_detail_thread_safe_logging_output_handler( | ||
| const rcutils_log_location_t * location, | ||
| int severity, | ||
| const char * name, | ||
| rcutils_time_point_value_t timestamp, | ||
| const char * format, | ||
| va_list * args) | ||
| { | ||
| try { | ||
| std::lock_guard<std::recursive_mutex> guard(rclpy::detail::get_global_logging_mutex()); | ||
| rcl_logging_multiple_output_handler(location, severity, name, timestamp, format, args); | ||
| } catch (std::exception & ex) { | ||
| RCUTILS_SAFE_FWRITE_TO_STDERR("rclpy failed to get the global logging mutex: "); | ||
| RCUTILS_SAFE_FWRITE_TO_STDERR(ex.what()); | ||
| RCUTILS_SAFE_FWRITE_TO_STDERR("\n"); | ||
| } catch (...) { | ||
| RCUTILS_SAFE_FWRITE_TO_STDERR("rclpy failed to get the global logging mutex\n"); | ||
| } | ||
| } | ||
| } // extern "C" |
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.
Uh oh!
There was an error while loading. Please reload this page.