Skip to content

Commit e533a17

Browse files
committed
Updating new comments to reflect new information.
1 parent 741e748 commit e533a17

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

docs/advanced/pycpp/utilities.rst

+6-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ redirects output to the corresponding Python streams:
5151

5252
The implementation in ``pybind11/iostream.h`` is NOT thread safe. Multiple
5353
threads writing to a redirected ostream concurrently cause data races
54-
and potentially buffer overflows. Therefore it is a requirement that
55-
all (possibly) concurrent redirected ostream writes are locked. Note
56-
that this is not expected to be an actual limitation, because without
57-
synchronization output will be randomly interleaved and most likely
58-
unreadable. Well-written C++ code is likely to use locking regardless of
59-
this pybind11 requirement. — For more background see the discussion under
60-
`PR #2982 <https://github.com/pybind/pybind11/pull/2982>`_.
54+
and potentially buffer overflows. Therefore it is currrently a requirement
55+
that all (possibly) concurrent redirected ostream writes are protected by
56+
a mutex. #HelpAppreciated: Work on iostream.h thread safety. For more
57+
background see the discussions under
58+
`PR #2982 <https://github.com/pybind/pybind11/pull/2982>`_ and
59+
`PR #2995 <https://github.com/pybind/pybind11/pull/2995>`_.
6160

6261
This method respects flushes on the output streams and will flush if needed
6362
when the scoped guard is destroyed. This allows the output to be redirected in

include/pybind11/iostream.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
BSD-style license that can be found in the LICENSE file.
88
99
WARNING: The implementation in this file is NOT thread safe. Multiple
10-
threads writing to a redirected ostream concurrently cause data races and
11-
potentially buffer overflows. Therefore it is a REQUIREMENT that all
12-
(possibly) concurrent redirected ostream writes are locked. For more
13-
background see the discussion under
14-
https://github.com/pybind/pybind11/pull/2982.
10+
threads writing to a redirected ostream concurrently cause data races
11+
and potentially buffer overflows. Therefore it is currrently a requirement
12+
that all (possibly) concurrent redirected ostream writes are protected by
13+
a mutex.
14+
#HelpAppreciated: Work on iostream.h thread safety.
15+
For more background see the discussions under
16+
https://github.com/pybind/pybind11/pull/2982 and
17+
https://github.com/pybind/pybind11/pull/2995.
1518
*/
1619

1720
#pragma once

tests/test_iostream.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ struct TestThread {
4141
static std::mutex cout_mutex;
4242
while (!stop_) {
4343
{
44+
// #HelpAppreciated: Work on iostream.h thread safety.
45+
// Without this lock, the clang ThreadSanitizer (tsan) reliably reports a
46+
// data race, and this test is predictably flakey on Windows.
47+
// For more background see the discussion under
48+
// https://github.com/pybind/pybind11/pull/2982 and
49+
// https://github.com/pybind/pybind11/pull/2995.
4450
const std::lock_guard<std::mutex> lock(cout_mutex);
4551
std::cout << "x" << std::flush;
4652
}

0 commit comments

Comments
 (0)