Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearing pending events on Socket instances #2426

Closed
tommikas opened this issue Aug 11, 2016 · 5 comments
Closed

Clearing pending events on Socket instances #2426

tommikas opened this issue Aug 11, 2016 · 5 comments

Comments

@tommikas
Copy link
Contributor

In blocking mode, for example:

  1. create, open and bind a UDP socket
  2. attach the socket to a callback function
  3. receiving a UDP packet on the listening port triggers the callback
  4. call recvfrom to retrieve the data
  5. an event seems to be pending, which blocks any subsequently received UDP packets from triggering the callback

In step 4, it seems that the _pending flag is cleared before socket_recvfrom() is called, which in turn triggers the event callback again setting the _pending flag again.

In non-blocking mode the recvfrom call in step 4 also triggers another callback, but a 2nd recvfrom call (which returns NSAPI_ERROR_WOULD_BLOCK) clears the flag, allowing new events to trigger the callback.

The same thing seems to happen with at least UDPSocket::recvfrom, TCPSocket::recv and TCPServer::accept.

Is this the intended behaviour? Have to use non-blocking sockets and make the call twice to clear the pending event to be able to receive new ones?

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 12, 2016

cc @geky

@geky
Copy link
Contributor

geky commented Aug 18, 2016

Thanks for raising the issue, unfortunately socket operations are currently disallowed from the Socket::attach function. Much like the existing attach functions in the mbed hal, this attach function may be in an interrupt context. This prevents long-running functions (including the mutexes used to synchronize the socket interface).

I posted an in depth explanation of the issue and available workarounds over here https://github.com/ARMmbed/mbed-os-morpheus/issues/167, here's the current options for working around the interrupt context:

At the moment, decoupling from interrupts is unfortunately frustrating for users, since we don't provide standard utilities to do so. We are currently looking into what these utilities might look like and how they might integrate with mbed-os.

There are a few options currently available:

  1. Use the blocking API

    The NetworkSocketAPI was originally intended to be used only in blocking mode, with multiple threads for multiple sockets. This avoids the issue of decoupling from interrupt contexts, and is simpler in the common case.

    This biggest issue with this option is the cost of creating multiple threads, which is especially noticable when using a socket as a server.

  2. Use semaphores to signal pending threads

    This is a good simple solution when dealing with multiple sockets:
    https://developer.mbed.org/teams/NetworkSocketAPI/code/HelloLWIPInterfaceNonBlocking/file/6563866f9466/main.cpp

    This lets you build equivalent functionality to BSD's select call, however composing different APIs can get complicated fairly quickly unless you execute each module in a separate thread.

  3. Use an event loop

    This is the most flexible option, although we don't have a standard implementation built into mbed-os. There is a prototype of an rtos-compatible event loop library being worked on here: https://github.com/armmbed/mbed-events

@tommikas
Copy link
Contributor Author

I'm aware that the callback function may run in interrupt context and shouldn't do anything too heavy. In retrospect I could've made it clearer, but step 4 in my original comment happens from thread context, not directly from the callback.

Anyway, just to be clear, it is intended behaviour that the call has to be made twice to clear the pending event? (In non-blocking mode and from thread context of course.)

@geky
Copy link
Contributor

geky commented Aug 19, 2016

Ah, I think I understand now.

This isn't disallowed behaviour, the attach function is defined to allow spurious callbacks to simplify network stack implementations:

The specified callback will be called on state changes such as when the socket can recv/send/accept successfully and on when an error occurs. The callback may also be called spuriously without reason.

That being said, thanks for pointing this out. We should probably move that flag so the event only triggers once for efficiency reasons.

@tommikas
Copy link
Contributor Author

It's a pretty minor thing though.

I was also thinking that arguably the application should be making the call until it returns NSAPI_ERROR_WOULD_BLOCK either way, in case multiple packets were received while the 'event pending" flag was up.
As it currently works sort of enforces that. Whether it should or not is a different matter...

artokin pushed a commit to artokin/mbed-os that referenced this issue Sep 2, 2020
…..91acece

91acece Remove test files
6568bc1 Merge branch 'release_internal' into release_external
4192cc8 Added configuration for RADIUS retry timer (ARMmbed#2438)
684b714 Added support for retries and multiple sockets to RADIUS client (ARMmbed#2426)
89e0ae0 WS: Restart auto CCA threshold after discovery (ARMmbed#2435)
dbb09b1 MAC/WS: Min possible Tack to 1ms and CCA interval to 2ms (ARMmbed#2434)
43b2ae2 Cca threshold test (ARMmbed#2436)
28108e1 Added device min sensitivity setting and stack information
7060c70 Cca threshold test (ARMmbed#2432)
640be71 WS: temporarily set default CCA threshold to -80 (ARMmbed#2431)
0a472ae WS: Calculate UFSI drift and trace (ARMmbed#2430)
61d3db8 Create APIs for DNS cache results
587add5 MAC: Validate TX time (ARMmbed#2429)
a1bfed4 Added typecast when computing max_timout from drift (ARMmbed#2428)
089fb3b Neighbour temporary entry update and Enhanced ACK tx update
70244f6 Wi-sun parameter and debug trace update
5752eae Created validate TX time handler (ARMmbed#2423)
022d61f Wi-sun Neighbour table update and DHCP new callback
857b41f Merge pull request ARMmbed#2421 from ARMmbed/update_from_mbed_os
1a9dd13 (via Mbed-OS)WS Management API missing include
4318f37 Calculate drift in critical state (ARMmbed#2419)
01a1909 FHSS WS: Do not use drift compensation with unpredictable linux timer (ARMmbed#2418)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 91acece
artokin pushed a commit to artokin/mbed-os that referenced this issue Sep 7, 2020
…..d879e6d

d879e6d Merge branch 'release_internal' into release_external
eef9246 Fixed network border router timeout recovery and EAPOL relay address fix
bac7ca6 Changed RADIUS MTU and small fixes
a9f8b75 Addeed support for DHCP vendor data
d8f0003 DHCPv6 functionality update
7fe0423 Added DHCPv6 vendor data generation for DNS queries
639f9db FHSS: Changed retry backoffs when no BC schedule or TX slots (ARMmbed#2440)
91acece Remove test files
6568bc1 Merge branch 'release_internal' into release_external
4192cc8 Added configuration for RADIUS retry timer (ARMmbed#2438)
684b714 Added support for retries and multiple sockets to RADIUS client (ARMmbed#2426)
89e0ae0 WS: Restart auto CCA threshold after discovery (ARMmbed#2435)
dbb09b1 MAC/WS: Min possible Tack to 1ms and CCA interval to 2ms (ARMmbed#2434)
43b2ae2 Cca threshold test (ARMmbed#2436)
28108e1 Added device min sensitivity setting and stack information
7060c70 Cca threshold test (ARMmbed#2432)
640be71 WS: temporarily set default CCA threshold to -80 (ARMmbed#2431)
0a472ae WS: Calculate UFSI drift and trace (ARMmbed#2430)
61d3db8 Create APIs for DNS cache results
587add5 MAC: Validate TX time (ARMmbed#2429)
a1bfed4 Added typecast when computing max_timout from drift (ARMmbed#2428)
089fb3b Neighbour temporary entry update and Enhanced ACK tx update
70244f6 Wi-sun parameter and debug trace update
5752eae Created validate TX time handler (ARMmbed#2423)
022d61f Wi-sun Neighbour table update and DHCP new callback
857b41f Merge pull request ARMmbed#2421 from ARMmbed/update_from_mbed_os
1a9dd13 (via Mbed-OS)WS Management API missing include
4318f37 Calculate drift in critical state (ARMmbed#2419)
01a1909 FHSS WS: Do not use drift compensation with unpredictable linux timer (ARMmbed#2418)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: d879e6d
artokin pushed a commit to artokin/mbed-os that referenced this issue Sep 7, 2020
…8609ae..d879e6d

d879e6d Merge branch 'release_internal' into release_external
eef9246 Fixed network border router timeout recovery and EAPOL relay address fix
bac7ca6 Changed RADIUS MTU and small fixes
a9f8b75 Addeed support for DHCP vendor data
d8f0003 DHCPv6 functionality update
7fe0423 Added DHCPv6 vendor data generation for DNS queries
639f9db FHSS: Changed retry backoffs when no BC schedule or TX slots (ARMmbed#2440)
91acece Remove test files
6568bc1 Merge branch 'release_internal' into release_external
4192cc8 Added configuration for RADIUS retry timer (ARMmbed#2438)
684b714 Added support for retries and multiple sockets to RADIUS client (ARMmbed#2426)
89e0ae0 WS: Restart auto CCA threshold after discovery (ARMmbed#2435)
dbb09b1 MAC/WS: Min possible Tack to 1ms and CCA interval to 2ms (ARMmbed#2434)
43b2ae2 Cca threshold test (ARMmbed#2436)
28108e1 Added device min sensitivity setting and stack information
7060c70 Cca threshold test (ARMmbed#2432)
640be71 WS: temporarily set default CCA threshold to -80 (ARMmbed#2431)
0a472ae WS: Calculate UFSI drift and trace (ARMmbed#2430)
61d3db8 Create APIs for DNS cache results
587add5 MAC: Validate TX time (ARMmbed#2429)
a1bfed4 Added typecast when computing max_timout from drift (ARMmbed#2428)
089fb3b Neighbour temporary entry update and Enhanced ACK tx update
70244f6 Wi-sun parameter and debug trace update
5752eae Created validate TX time handler (ARMmbed#2423)
022d61f Wi-sun Neighbour table update and DHCP new callback
857b41f Merge pull request ARMmbed#2421 from ARMmbed/update_from_mbed_os
1a9dd13 (via Mbed-OS)WS Management API missing include
4318f37 Calculate drift in critical state (ARMmbed#2419)
01a1909 FHSS WS: Do not use drift compensation with unpredictable linux timer (ARMmbed#2418)

git-subtree-dir: connectivity/nanostack/sal-stack-nanostack
git-subtree-split: d879e6d
winneymj pushed a commit to winneymj/mbed-os that referenced this issue Sep 29, 2020
…8609ae..d879e6d

d879e6d Merge branch 'release_internal' into release_external
eef9246 Fixed network border router timeout recovery and EAPOL relay address fix
bac7ca6 Changed RADIUS MTU and small fixes
a9f8b75 Addeed support for DHCP vendor data
d8f0003 DHCPv6 functionality update
7fe0423 Added DHCPv6 vendor data generation for DNS queries
639f9db FHSS: Changed retry backoffs when no BC schedule or TX slots (ARMmbed#2440)
91acece Remove test files
6568bc1 Merge branch 'release_internal' into release_external
4192cc8 Added configuration for RADIUS retry timer (ARMmbed#2438)
684b714 Added support for retries and multiple sockets to RADIUS client (ARMmbed#2426)
89e0ae0 WS: Restart auto CCA threshold after discovery (ARMmbed#2435)
dbb09b1 MAC/WS: Min possible Tack to 1ms and CCA interval to 2ms (ARMmbed#2434)
43b2ae2 Cca threshold test (ARMmbed#2436)
28108e1 Added device min sensitivity setting and stack information
7060c70 Cca threshold test (ARMmbed#2432)
640be71 WS: temporarily set default CCA threshold to -80 (ARMmbed#2431)
0a472ae WS: Calculate UFSI drift and trace (ARMmbed#2430)
61d3db8 Create APIs for DNS cache results
587add5 MAC: Validate TX time (ARMmbed#2429)
a1bfed4 Added typecast when computing max_timout from drift (ARMmbed#2428)
089fb3b Neighbour temporary entry update and Enhanced ACK tx update
70244f6 Wi-sun parameter and debug trace update
5752eae Created validate TX time handler (ARMmbed#2423)
022d61f Wi-sun Neighbour table update and DHCP new callback
857b41f Merge pull request ARMmbed#2421 from ARMmbed/update_from_mbed_os
1a9dd13 (via Mbed-OS)WS Management API missing include
4318f37 Calculate drift in critical state (ARMmbed#2419)
01a1909 FHSS WS: Do not use drift compensation with unpredictable linux timer (ARMmbed#2418)

git-subtree-dir: connectivity/nanostack/sal-stack-nanostack
git-subtree-split: d879e6d
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

No branches or pull requests

3 participants