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

fix: stuck MJPEG clients when one client fails to initialize #676

Merged
merged 1 commit into from
Dec 31, 2024

Conversation

vbarbaresi
Copy link
Contributor

@vbarbaresi vbarbaresi commented Dec 31, 2024

Problem

There was an issue with the Client implementation: if a socket started a connection on the internal MJPEG port and was never initialized, the entire server thread would be stuck in the while loop waiting for the client to send something.

This broke other clients connected to the server: no frames would be received anymore.

To reproduce on emulator / real device:

  • Start an Appium server with mJPEG server enabled (appium:mjpegServerPort:9100 for instance)
  • Start an Appium session that reads the mJPEG frames (using Appium Inspector for instance)
  • Then from adb shell: connect to the internal mJPEG server (default port 7810): nc localhost 7810
  • --> Streaming will be broken for the existing clients
  • Another way to reproduce instead of using the phone shell would be from the host using the forwarded MJPEG port: nc localhost 9100

This happens because the client connection is not sending a proper HTTP request: it simply tries to receive data and this wasn't supported.
In this particular case we should not wait forever and not stop streaming frames to other clients

Fix details

This patch fixes the problem by returning early: if the client is sending anything, initialization will be retried later if necessary instead of blocking the thread.

Another side effect is that we now actually receive frames usingnc localhost 9100: this command was previously stuck waiting for the client to send something.
This can be useful for checking the status of the MJPEG server if we don't have a proper HTTP client handy

I added a unit test reproducing the bug by opening a socket connection to the MJPEG server and closing it without sending any data. Then verified that the server still works for another HTTP client. This test wouldn't pass before the fix

There was an issue with the Client implementation: if a socket started a connection on the internal MJPEG port and was never initialized,
the entire server thread would be stuck in the while loop waiting for initialization.
This broke other clients connected to the server: no frames would be received anymore.

To reproduce on emulator / real device:
- start an Appium server with mJPEG server enabled (`appium:mjpegServerPort:9100` for instance)
- Start an Appium session that reads the mJPEG frames (using Appium Inspector for instance)
- Then from `adb shell`: connect to the internal mJPEG server (default port 7810):
`nc localhost 7810`
- --> Streaming will be broken for the existing clients

This fixes the problem by returning early: if the client is not ready initialization will be retried later if necessary (or never) instead of blocking the thread

Another side effect is that we actually receive frames from `nc localhost 7810` instead of it being stuck
Copy link

linux-foundation-easycla bot commented Dec 31, 2024

CLA Signed


The committers listed above are authorized under a signed CLA.

@vbarbaresi vbarbaresi changed the title Fix stuck client when socket client never initializes Fix stuck clients when one client fails to initialize Dec 31, 2024
@vbarbaresi vbarbaresi changed the title Fix stuck clients when one client fails to initialize Fix stuck MJPEG clients when one client fails to initialize Dec 31, 2024
@@ -0,0 +1,75 @@
package io.appium.uiautomator2.server.mjpeg;
Copy link
Member

Choose a reason for hiding this comment

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

👍

@KazuCocoa KazuCocoa changed the title Fix stuck MJPEG clients when one client fails to initialize fix: stuck MJPEG clients when one client fails to initialize Dec 31, 2024
@mykola-mokhnach mykola-mokhnach merged commit 34161a3 into appium:master Dec 31, 2024
10 of 11 checks passed
github-actions bot pushed a commit that referenced this pull request Dec 31, 2024
## [7.1.9](v7.1.8...v7.1.9) (2024-12-31)

### Bug Fixes

* stuck client when socket client never initializes ([#676](#676)) ([34161a3](34161a3))
Copy link

🎉 This PR is included in version 7.1.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

@jlipps
Copy link
Member

jlipps commented Jan 8, 2025

Hi @vbarbaresi, congrats: the Appium project wants to compensate you for this contribution! Please reply to this comment mentioning @jlipps and @KazuCocoa and sharing your OpenCollective account name, so that we can initiate payment! Or let us know if you decline to receive compensation via OpenCollective. Thank you!

@vbarbaresi
Copy link
Contributor Author

Hi, thanks for the proposition, it's much appreciated!
Unfortunately the contribution was made on behalf of the company so I have to decline the compensation.
I'm glad I contributed and I hope to be able to contribute again in the future!

@jlipps
Copy link
Member

jlipps commented Jan 9, 2025

Thanks for letting us know, hope to see you again @vbarbaresi :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released size:S contribution size: S
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants