-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
[core] Implement dup2 wrapper #50439
Merged
Merged
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
75db43a
dup2 wrapper
dentiny 661e519
fix windows
dentiny 0ec061e
rename to scoped
dentiny 6c3287e
rename
dentiny 11d57d0
doc
dentiny 6641c3f
fix
dentiny 355e119
Merge branch 'master' into hjiang/dup2-with-restore
dentiny e5d0d5d
fix windows
dentiny 957d04c
fix windoes
dentiny 8961742
Merge branch 'hjiang/dup2-with-restore' of github.com:dentiny/ray int…
dentiny af3b594
fix windows.....
dentiny c49b7f5
Merge branch 'master' into hjiang/dup2-with-restore
dentiny c54e44a
Merge branch 'master' into hjiang/dup2-with-restore
dentiny 04cc0e1
windows
dentiny e0aac6d
IWYU
dentiny 666268a
fix windows
dentiny 808fd26
fix windows
dentiny d991919
windows
dentiny 267ece0
fix windows
dentiny ba7b90c
Merge branch 'master' into hjiang/dup2-with-restore
dentiny 2c22044
lint
dentiny 60e2e27
duck type comment
dentiny e384f27
linter
dentiny 9a50f22
Merge branch 'master' into hjiang/dup2-with-restore
dentiny 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 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 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 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 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 |
---|---|---|
|
@@ -325,3 +325,16 @@ ray_cc_library( | |
"@com_google_absl//absl/synchronization", | ||
], | ||
) | ||
|
||
ray_cc_library( | ||
name = "dup2_wrapper", | ||
hdrs = ["dup2_wrapper.h"], | ||
srcs = select({ | ||
"@platforms//os:windows": ["dup2_wrapper_windows.cc"], | ||
"//conditions:default": ["dup2_wrapper_posix.cc"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the difference between this approach and the other approach the current codebase is using which is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No difference,
|
||
}), | ||
deps = [ | ||
":compat", | ||
":logging", | ||
], | ||
) |
This file contains 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 2025 The Ray Authors. | ||
// | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
#include "ray/util/compat.h" | ||
|
||
namespace ray { | ||
|
||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class ScopedDup2Wrapper { | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
// Duplicate [oldfd] to [newfd], same semantics with syscall `dup2`. | ||
static std::unique_ptr<ScopedDup2Wrapper> New(MEMFD_TYPE_NON_UNIQUE oldfd, | ||
MEMFD_TYPE_NON_UNIQUE newfd); | ||
|
||
ScopedDup2Wrapper(const ScopedDup2Wrapper &) = delete; | ||
ScopedDup2Wrapper &operator=(const ScopedDup2Wrapper &) = delete; | ||
|
||
// Restore oldfd. | ||
~ScopedDup2Wrapper(); | ||
|
||
private: | ||
ScopedDup2Wrapper(MEMFD_TYPE_NON_UNIQUE oldfd, MEMFD_TYPE_NON_UNIQUE restorefd) | ||
: oldfd_(oldfd), restorefd_(restorefd) {} | ||
|
||
MEMFD_TYPE_NON_UNIQUE oldfd_; | ||
MEMFD_TYPE_NON_UNIQUE restorefd_; | ||
}; | ||
|
||
} // namespace ray |
This file contains 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 2025 The Ray Authors. | ||
// | ||
// 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 <unistd.h> | ||
|
||
#include <cstring> | ||
|
||
#include "ray/util/dup2_wrapper.h" | ||
#include "ray/util/logging.h" | ||
|
||
namespace ray { | ||
|
||
/*static*/ std::unique_ptr<ScopedDup2Wrapper> ScopedDup2Wrapper::New(int oldfd, | ||
int newfd) { | ||
const int restorefd = dup(oldfd); | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RAY_CHECK_NE(restorefd, -1) << "Fails to duplicate oldfd " << oldfd << " because " | ||
<< strerror(errno); | ||
|
||
const int ret = dup2(oldfd, newfd); | ||
RAY_CHECK_NE(ret, -1) << "Fails to duplicate oldfd " << oldfd << " to " << newfd | ||
<< " because " << strerror(errno); | ||
|
||
return std::unique_ptr<ScopedDup2Wrapper>(new ScopedDup2Wrapper(oldfd, restorefd)); | ||
} | ||
|
||
ScopedDup2Wrapper::~ScopedDup2Wrapper() { | ||
const int ret = dup2(oldfd_, restorefd_); | ||
RAY_CHECK_NE(ret, -1) << "Fails to duplicate oldfd " << oldfd_ << " to " << restorefd_ | ||
<< " because " << strerror(errno); | ||
} | ||
|
||
} // namespace ray |
This file contains 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,54 @@ | ||
// Copyright 2025 The Ray Authors. | ||
// | ||
// 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 "ray/util/dup2_wrapper.h" | ||
|
||
namespace ray { | ||
|
||
/*static*/ std::unique_ptr<ScopedDup2Wrapper> ScopedDup2Wrapper::New(HANDLE oldfd, | ||
HANLE newfd) { | ||
HANDLE restorefd = NULL; | ||
BOOL success = DuplicateHandle(GetCurrentProcess(), | ||
oldfd, | ||
GetCurrentProcess(), | ||
&restorefd, | ||
0, | ||
FALSE, | ||
DUPLICATE_SAME_ACCESS); | ||
RAY_CHECK(success); | ||
|
||
success = DuplicateHandle(GetCurrentProcess(), | ||
oldfd, | ||
GetCurrentProcess(), | ||
&newfd, | ||
0, | ||
FALSE, | ||
DUPLICATE_SAME_ACCESS); | ||
RAY_CHECK(success); | ||
|
||
return std::unique_ptr<ScopedDup2Wrapper>(new ScopedDup2Wrapper(oldfd, restorefd)); | ||
} | ||
|
||
ScopedDup2Wrapper::~ScopedDup2Wrapper() { | ||
BOOL success = DuplicateHandle(GetCurrentProcess(), | ||
oldfd_, | ||
GetCurrentProcess(), | ||
&restorefd_, | ||
0, | ||
FALSE, | ||
DUPLICATE_SAME_ACCESS); | ||
RAY_CHECK(success); | ||
} | ||
|
||
} // namespace ray |
This file contains 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 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,84 @@ | ||
// Copyright 2025 The Ray Authors. | ||
// | ||
// 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 "ray/util/dup2_wrapper.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <string_view> | ||
|
||
#include "ray/common/test/testing.h" | ||
#include "ray/util/compat.h" | ||
#include "ray/util/filesystem.h" | ||
#include "ray/util/temporary_directory.h" | ||
|
||
#if defined(__APPLE__) || defined(__linux__) | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#elif defined(_WIN32) | ||
#include <windows.h> | ||
#endif | ||
|
||
namespace ray { | ||
|
||
namespace { | ||
|
||
constexpr std::string_view kContent = "helloworld\n"; | ||
|
||
TEST(ScopedDup2WrapperTest, BasicTest) { | ||
ScopedTemporaryDirectory temp_dir; | ||
const auto dir = temp_dir.GetDirectory(); | ||
const auto path = dir / "test_file"; | ||
const std::string path_string = path.string(); | ||
|
||
#if defined(__APPLE__) || defined(__linux__) | ||
int fd = open(path_string.data(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); | ||
ASSERT_NE(fd, -1); | ||
#elif defined(_WIN32) | ||
HANDLE fd = CreateFile(path_string, // File name | ||
GENERIC_READ | GENERIC_WRITE, // Access mode: read/write | ||
0, // No sharing | ||
NULL, // Default security attributes | ||
OPEN_ALWAYS, // Open file if it exists, create if it doesn't | ||
FILE_ATTRIBUTE_NORMAL, // File attributes | ||
NULL); // No template file | ||
|
||
// Check if the file was successfully opened or created | ||
ASSERT_NE(fd, INVALID_HANDLE_VALUE); | ||
#endif | ||
|
||
{ | ||
auto dup2_wrapper = ScopedDup2Wrapper::New(/*oldfd=*/fd, /*newfd=*/GetStderrHandle()); | ||
|
||
// Write to stdout should appear in file. | ||
std::cerr << kContent << std::flush; | ||
const auto actual_content = ReadEntireFile(path_string); | ||
RAY_ASSERT_OK(actual_content); | ||
EXPECT_EQ(*actual_content, kContent); | ||
} | ||
|
||
testing::internal::CaptureStderr(); | ||
std::cerr << kContent << std::flush; | ||
const std::string stderr_content = testing::internal::GetCapturedStderr(); | ||
EXPECT_EQ(stderr_content, kContent); | ||
|
||
// Not changed since last write. | ||
const auto actual_content = ReadEntireFile(path_string); | ||
RAY_ASSERT_OK(actual_content); | ||
EXPECT_EQ(*actual_content, kContent); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace ray |
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's the difference between StatusString and ToString
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.
Functionality-wise no difference, the reason to have this function is for pretty print status for GTEST macros.
In nuro, we have another way to handle:
Having a
StatusString
is easier to implement.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.
Could you elaborate, I don't think I get it. Why cannot the caller call
ToString()
?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.
We use it in the macro
it's a macro thus duck typing, so we have to have the same function name for both
Status
andStatusOr
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.
Calling
ToString
is OK forStatus
, but improper forStatusOr
, because we haven't implement the stringify function for value-typeThere 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.
Got it. Please turn the above explanation to a comment so people know why you need two.
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.
Why need a comment here? Exposing status stringify seems natural to me.
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.
Anyway I added a comment for necessity from duck-type template/macro.