[PATCH] tst-fopen-threaded: Only check EOF for failing read
Siddhesh Poyarekar
siddhesh@sourceware.org
Fri Mar 14 14:40:50 GMT 2025
The fread race checker looks for EOF in every thread, which is incorrect
since threads calling fread successfully could lag behind and read the
EOF condition, resulting in multiple threads thinking that they
encountered an EOF.
Only look for EOF condition if fread fails to read a char. Also check
for error in the stream for completeness.
Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
---
I'm trying to find a machine to test this patch on, but it seems like a
trivial fix.
sysdeps/pthread/tst-fopen-threaded.c | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/sysdeps/pthread/tst-fopen-threaded.c b/sysdeps/pthread/tst-fopen-threaded.c
index 5c792c93e3..053587a36b 100644
--- a/sysdeps/pthread/tst-fopen-threaded.c
+++ b/sysdeps/pthread/tst-fopen-threaded.c
@@ -64,19 +64,22 @@ threadReadRoutine (void *argv)
/* Wait for all threads to be ready to read. */
xpthread_barrier_wait (&barrier);
- ret =
- fread (&read_buffer, sizeof (char), sizeof (read_buffer), my_data->fd);
- if (feof (my_data->fd) != 0)
+ ret = fread (&read_buffer, sizeof (char), sizeof (read_buffer), my_data->fd);
+ /* If no data is returned, look for EOF flag and record it in MY_DATA.
+ Successful readers could still see the EOF if they fall behind the failing
+ read when calling feof(), which could result in a false negative. To
+ avoid this race, we only make the failing reader check for EOF or
+ error. */
+ if (ret == 0)
{
- clearerr (my_data->fd);
- my_data->eof = true;
+ if (feof (my_data->fd) != 0)
+ my_data->eof = true;
+ else
+ FAIL_EXIT1 ("fread failed (ferror: %d): %m", ferror (my_data->fd));
}
else
- {
- TEST_COMPARE (ret, 1);
- /* Save the read value. */
- my_data->value = read_buffer;
- }
+ /* Save the read value. */
+ my_data->value = read_buffer;
TEST_COMPARE (ferror (my_data->fd), 0);
return NULL;
}
--
2.48.1
More information about the Libc-alpha
mailing list