From 2b76a4fafa91dd531e35084ab544d9cd8a3e2d79 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Thu, 15 May 2025 00:16:25 -0700 Subject: [PATCH] unix: fix uv_thread_{get,set}priority error codes (#4782) pthread_getschedparam and pthread_setschedparam do not use errno but instead return the error code directly. https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_getschedparam.html Also include a fix to return UV_ESRCH instead of UV_EBADF on Windows, like Unix systems do. --- src/unix/core.c | 6 +++--- src/win/util.c | 18 ++++++++++++++---- test/test-thread-priority.c | 6 ++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/unix/core.c b/src/unix/core.c index bd51b69b8..1dde27bd7 100644 --- a/src/unix/core.c +++ b/src/unix/core.c @@ -1656,7 +1656,7 @@ int uv_thread_getpriority(uv_thread_t tid, int* priority) { r = pthread_getschedparam(tid, &policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); #ifdef __linux__ if (SCHED_OTHER == policy && pthread_equal(tid, pthread_self())) { @@ -1709,7 +1709,7 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { r = pthread_getschedparam(tid, &policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); #ifdef __linux__ /** @@ -1757,7 +1757,7 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { param.sched_priority = prio; r = pthread_setschedparam(tid, policy, ¶m); if (r != 0) - return UV__ERR(errno); + return UV__ERR(r); } return 0; diff --git a/src/win/util.c b/src/win/util.c index 57061bf8d..da1238d37 100644 --- a/src/win/util.c +++ b/src/win/util.c @@ -1519,20 +1519,26 @@ int uv_os_setpriority(uv_pid_t pid, int priority) { } int uv_thread_getpriority(uv_thread_t tid, int* priority) { + DWORD err; int r; if (priority == NULL) return UV_EINVAL; r = GetThreadPriority(tid); - if (r == THREAD_PRIORITY_ERROR_RETURN) - return uv_translate_sys_error(GetLastError()); + if (r == THREAD_PRIORITY_ERROR_RETURN) { + err = GetLastError(); + if (err == ERROR_INVALID_HANDLE) + return UV_ESRCH; + return uv_translate_sys_error(err); + } *priority = r; return 0; } int uv_thread_setpriority(uv_thread_t tid, int priority) { + DWORD err; int r; switch (priority) { @@ -1555,8 +1561,12 @@ int uv_thread_setpriority(uv_thread_t tid, int priority) { return 0; } - if (r == 0) - return uv_translate_sys_error(GetLastError()); + if (r == 0) { + err = GetLastError(); + if (err == ERROR_INVALID_HANDLE) + return UV_ESRCH; + return uv_translate_sys_error(err); + } return 0; } diff --git a/test/test-thread-priority.c b/test/test-thread-priority.c index 0aaf29772..f1e4f9a70 100644 --- a/test/test-thread-priority.c +++ b/test/test-thread-priority.c @@ -101,5 +101,11 @@ TEST_IMPL(thread_priority) { uv_sem_destroy(&sem); + /* Now that the thread no longer exists, verify that the relevant error is returned */ +#if !defined(__ANDROID__) + ASSERT_EQ(UV_ESRCH, uv_thread_getpriority(task_id, &priority)); + ASSERT_EQ(UV_ESRCH, uv_thread_setpriority(task_id, UV_THREAD_PRIORITY_LOWEST)); +#endif + return 0; } \ No newline at end of file