llvm / llvm/llvm-project

[libcxx] copy_file enters an infinite loop in a multithreaded environment.

Open
#169,261 4 comments 0 reactions 0 assignees View on GitHub
filesystem libc++
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When I use multiple threads to copy a file without locking, there are occasional instances where the `copy_file` operation seems to get stuck indefinitely.
By adding print logs before and after calling `copy_file`, it was discovered that the process was getting stuck during the call to `copy_file_impl_copy_file_range`.The main body of `copy_file_impl_copy_file_range` is a loop.
```
do {
ssize_t res;

if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {
ec = capture_errno();
return false;
}
count -= res;
} while (count > 0);
```
In the above scenario, the value of res was returned as 0. It appears that one of the two threads was continuously copying empty data.Why does this situation occur? Should we add a condition to check if res is 0 to prevent entering an infinite loop?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.