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

Update IOSDeviceManager.ts #1392

Closed
wants to merge 7 commits into from
Closed

Conversation

erdncyz
Copy link
Contributor

@erdncyz erdncyz commented Sep 23, 2024

During port selection, even if a port appears to be available, it may later be used by another process. In particular, race conditions can occur when ports such as wdaLocalPort or mjpegServerPort are being used simultaneously by another process.

Improvements Made:

Port Usage Control: Functions getFreePortWithCheck and getUnusedPort were added to more accurately check whether ports are available. This is an important step to prevent port conflicts. Safe Port Selection: By using getUnusedPort, safe port selection is ensured for wdaLocalPort and mjpegServerPort. Error Handling and Logging: Parts related to checking port availability and logging errors have been improved. With this code, the risk of port conflicts has been reduced, and port management is now handled in a more reliable way.

#1340

Improvements Made:
Port Usage Control: Functions getFreePortWithCheck and getUnusedPort were added to more accurately check whether ports are available. This is an important step to prevent port conflicts.
Safe Port Selection: By using getUnusedPort, safe port selection is ensured for wdaLocalPort and mjpegServerPort.
Error Handling and Logging: Parts related to checking port availability and logging errors have been improved.
With this code, the risk of port conflicts has been reduced, and port management is now handled in a more reliable way.

#1340
Copy link

vercel bot commented Sep 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
appium-device-farm-bre4 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 9:58am

review comment edit
@saikrishna321
Copy link
Member

@erdncyz This will still not help solve the issue.

When the server starts we pick all the devices and get a freeport and assign to the device object. At the time of assigning the port is available, issue comes up with the session creation happens. At this time the port is already picked by some other process.

Fix: During session creation for iOS you need to check if the port is free, if not get another free port and re-assign to the device object.

@erdncyz
Copy link
Contributor Author

erdncyz commented Sep 25, 2024

Hi @saikrishna321 @SrinivasanTarget @sudharsan-selvaraj ,

ı change my code for your comment @saikrishna321 can you check again please.

  1. getFreePortWithCheck function:
    Purpose: This function finds a free, unused port on the system.
    How it works:
    It creates a TCP server (net.createServer()) and listens on port 0. When you pass 0 as the port number, the system automatically selects a free port for you.
    Once the server starts listening, it retrieves the port that was assigned by the system (using server.address().port), and then immediately closes the server (server.close()), meaning it no longer holds onto that port.
    The port number is then returned as a result via a promise.
    Why unref() is used: server.unref() prevents the server from keeping the Node.js process alive. This is useful to ensure that even if the server is open briefly, it won’t prevent the program from exiting if there is no other work left to do.
  2. checkIfPortIsAvailable function:
    Purpose: This function checks whether a specific port is available (i.e., not already being used by another process).
    How it works:
    It tries to create a TCP server that listens on the specified port.
    If the server successfully starts listening on the port, it means the port is available, and the function resolves to true.
    If there is an error while attempting to listen (because another process is already using the port), it catches the error and resolves to false, indicating that the port is in use.
    After determining the port’s availability, the server is immediately closed (tester.close()), regardless of whether the port was available or not.
    Why this is important: Since the system might assign an available port, but by the time you use it, another process could have taken it, this function ensures that the port is still available at the moment you need it.
  3. getUnusedPortAtSessionCreation function:
    Purpose: This function combines the other two functions to continuously find and verify an available port, ensuring it's not being used by another process.

@erdncyz erdncyz closed this by deleting the head repository Sep 26, 2024
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