Skip to content

network-windows: Fix race condition in wait_cancellable - #1398

Merged
dNechita merged 1 commit into
mainfrom
fix-wait-for-socket
Mar 4, 2026
Merged

dNechita merged 1 commit into
mainfrom
fix-wait-for-socket

Conversation

@RubenNatanael

@RubenNatanael RubenNatanael commented Feb 19, 2026 •

Copy link
Copy Markdown
Collaborator

PR Description

Multiple threads calling wait_cancellable (e.g., one reading, one writing) were overwriting the socket's WSAEventSelect configuration.

This commit adds a CriticalSection and waiter-counting logic to:

  • Ensure FD_READ and FD_WRITE are merged rather than overwritten.
  • Protect the shared event object reset and selection logic.
  • Defer decrementing waiter counts until after the wait to prevent prematurely clearing the socket event mask.

Edit:
Issue: #1402

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have commented new code, particularly complex or unclear areas
  • I have checked that I did not introduce new warnings or errors (CI output)
  • I have checked that components that use libiio did not get broken
  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)

@RubenNatanael RubenNatanael changed the title Fix race condition in wait_cancellable network-windows: Fix race condition in wait_cancellable Feb 19, 2026
@RubenNatanael RubenNatanael reopened this Feb 23, 2026
@rgetz

rgetz commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

it would likely be better if you re-used the primitives in lock-windows.c.

Comment thread network.h Outdated
struct iiod_client_pdata {
int fd;

#ifdef _WIN32

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd maintain platform independence for this file. The existing architecture consists of a single header file (network.h), a core implementation file (network.c) containing cross-platform code (with some exceptions), and separate platform-specific implementation files (network-windows.c and network-unix.c).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification done, let me know if it's ok now!

@RubenNatanael
RubenNatanael force-pushed the fix-wait-for-socket branch 2 times, most recently from 195e1c7 to 17a8dc7 Compare February 25, 2026 13:50
Comment thread network-windows.c Outdated
return -ENOMEM;
}

struct iio_event_sync *sync = (struct iio_event_sync*)malloc(sizeof(struct iio_event_sync));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need something here in case the malloc fails?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, thanks.

@RubenNatanael
RubenNatanael force-pushed the fix-wait-for-socket branch 2 times, most recently from df850cd to 7937073 Compare February 26, 2026 09:17

@dNechita dNechita left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@rgetz

rgetz commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Do you actually see a failure? or a trace from thread santizer on real application? Or is this a theoretical fix?

@RubenNatanael

Copy link
Copy Markdown
Collaborator Author

Do you actually see a failure? or a trace from thread santizer on real application? Or is this a theoretical fix?

Hi @rgetz, the bug is as real as it gets. I found it while using iio_info V1; sometimes the command gets stuck. After some debugging and adding logs, I discovered that the program hangs in wait_cancelable, a function called by every task that uses the socket.
More specifically, it happens when two threads call this function at the same time. The last thread to call it overwrites the entire mask, leaving the first task waiting forever. This occurs because without the mask, nothing triggers a signal to alert the task that the socket is ready.
After a few days, I also found an issue on GitHub complaining about a similar bug. I asked the user to test my changes, and they confirmed the problem was resolved. You can find the issue here: #1402

@rgetz

rgetz commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

the bug is as real as it gets. I found it while using iio_info V1;

that's important to put into the commit message - how to reproduce the failure that you are seeing - so that others in the future understand the impact if they think they will see similar things, or if someone decides they need to revert this to fix something else.

Comment thread network-windows.c
if (!os_priv) {
goto err_close_events;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think normal libiio coding style is that you don't need {} around one line statements.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you so much for the advices!

@RubenNatanael
RubenNatanael force-pushed the fix-wait-for-socket branch 2 times, most recently from ffd29fc to 5a5ce39 Compare March 2, 2026 14:35
Comment thread network-windows.c
free(os_priv);
goto err_close_events;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also similar here - it's more like other functions to move the free(os_priv); into the error/return path (at a new err_free_os_priv (or whatever your prefer), right before the existing err_close_events: label, and then in the above - you have a single goto without {}.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

Multiple threads calling wait_cancellable (e.g., one reading, one writing) were overwriting the socket's WSAEventSelect configuration.
The bug appears during tests performed on ZC702 and ZC706 boards
(possibly others as well) approximately 30% of the time when, for
example, iio_info is executed (or any other program that creates a
device context). For more details, you can refer to issue
(#1402).
Signed-off-by: Ruben Mujdei <ruben.mujdei@analog.com>
@dNechita
dNechita merged commit 3ed608c into main Mar 4, 2026
38 checks passed
@dNechita
dNechita deleted the fix-wait-for-socket branch March 4, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants