Skip to content

Commit 76b925e

Browse files
committed
Fix stdin handling in CUFileDriverScope
- adds stdin backup and restore mechanism to prevent issues with file descriptor handling when using cuFile library. This fixes a problem where cuFileDriverOpen could close stdin, causing subsequent GDS file operations to fail when the file descriptor 0 is reused. Signed-off-by: Janusz Lisiecki <[email protected]>
1 parent c97c96f commit 76b925e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

dali/operators/reader/gds_mem_test.cu

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,23 @@ namespace test {
3232

3333
struct CUFileDriverScope {
3434
CUFileDriverScope() {
35+
// cuFileDriverOpen is some versions of cuFile library, can close stdin
36+
// returning 0 file descriptor to the pool, then dali gets it from the OS opening a file
37+
// and passing to GDS which cannot handle it properly leading to an error
38+
int stdin_backup = dup(STDIN_FILENO);
39+
if (stdin_backup == -1) {
40+
std::cerr << "dup failed: " << strerror(errno) << "\n";
41+
}
3542
CUDA_CALL(cuFileDriverOpen());
43+
if (stdin_backup != -1) {
44+
if (fcntl(STDIN_FILENO, F_GETFL) == -1 && errno == EBADF) {
45+
// Restore stdin from backup
46+
if (dup2(stdin_backup, STDIN_FILENO) == -1) {
47+
std::cerr << "dup2 failed: " << strerror(errno) << "\n";
48+
}
49+
}
50+
close(stdin_backup); // Cleanup backup
51+
}
3652
}
3753
~CUFileDriverScope() {
3854
// Here we're the sole owner of cuFile library - we can (and want to) call cuFileDriverClose

dali/util/cufile_helper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,23 @@ struct CUFileDriverScope {
3737
CUFileDriverScope() {
3838
// v2 API performs proper reference counting, so we increase the reference count here...
3939
if (cuFileIsSymbolAvailable("cuFileDriverClose_v2")) {
40+
// cuFileDriverOpen is some versions of cuFile library, can close stdin
41+
// returning 0 file descriptor to the pool, then dali gets it from the OS opening a file
42+
// and passing to GDS which cannot handle it properly leading to an error
43+
int stdin_backup = dup(STDIN_FILENO);
44+
if (stdin_backup == -1) {
45+
std::cerr << "dup failed: " << strerror(errno) << "\n";
46+
}
4047
CUDA_CALL(cuFileDriverOpen());
48+
if (stdin_backup != -1) {
49+
if (fcntl(STDIN_FILENO, F_GETFL) == -1 && errno == EBADF) {
50+
// Restore stdin from backup
51+
if (dup2(stdin_backup, STDIN_FILENO) == -1) {
52+
std::cerr << "dup2 failed: " << strerror(errno) << "\n";
53+
}
54+
}
55+
close(stdin_backup); // Cleanup backup
56+
}
4157
}
4258
}
4359
~CUFileDriverScope() {

0 commit comments

Comments
 (0)