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

write EPIPE Error Encountered During Appium Server Shutdown When Using withLogFile #2238

Open
dipakkumar1225 opened this issue Oct 3, 2024 · 4 comments

Comments

@dipakkumar1225
Copy link

dipakkumar1225 commented Oct 3, 2024

During shutdown, I encounter the following error: uncaughtException: write EPIPE

Environment

  • Java client build version : 9.3.0
  • Appium server version : 2.11.2
  • Desktop OS/version : Mac Sequoia 15.0
  • Node.js : 20.15.0
  • Mobile platform/version under test: Android 14 (Motorola edge 30)
  • Real device or emulator/simulator: Real Device

Details

I have attempted to resolve this by using clearOutPutStreams(), disabling console logging, and ensuring log streams are flushed and closed, but the error persists.

Code To Reproduce Issue

public class AppiumServerManager {

    private final int DEFAULT_APPIUM_SERVER_PORT = 4723;
    private final AppiumDriverLocalService appiumDriverLocalService;
    private final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private boolean isAppiumRunning = false;

    public AppiumServerManager(){
        appiumDriverLocalService = getAppiumDriverLocalService();
        appiumDriverLocalService.clearOutPutStreams();
    }

    private AppiumDriverLocalService getAppiumDriverLocalService() {
        return AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                .withIPAddress("127.0.0.1")
                .withArgument(() -> "--long-stacktrace")
                .usingPort(DEFAULT_APPIUM_SERVER_PORT)
                .withArgument(GeneralServerFlag.LOG_LEVEL, DebugFlag.DEBUG_DEBUG.getArgument())
                .withArgument(GeneralServerFlag.USE_DRIVERS, "uiautomator2")
                .withArgument(() -> "-ka", "800")
                .withTimeout(Duration.ofSeconds(30))
                .withLogFile(new File(System.getProperty("user.dir") + File.separator + "appium_logs_" + System.currentTimeMillis() + ".log"))
        );
    }

    public void startAppiumServer() {
        if (!isAppiumServerRunning()) {
            appiumDriverLocalService.start();
            isAppiumRunning = true;
            System.out.println(getCurrentTimestamp() + " - INFO: Appium Server Started on port " + DEFAULT_APPIUM_SERVER_PORT);
        } else {
            System.out.println(getCurrentTimestamp() + " - WARNING: Appium Server is already running.");
        }
    }

    public void stopAppiumServer() {
        try {
            if (isAppiumRunning) {
                System.out.println(getCurrentTimestamp() + " - INFO: Stopping Appium server with a delay...");
                Thread.sleep(1000);  // 2 second delay before fully stopping
                appiumDriverLocalService.stop();
                isAppiumRunning = false;
                System.out.println(getCurrentTimestamp() + " - INFO: Appium Server Stopped.");
            }
        } catch (InterruptedException e) {
            System.err.println("Error during shutdown delay: " + e.getMessage());
        }
    }

    private boolean isAppiumServerRunning() {
        if (appiumDriverLocalService == null) {
            System.out.println(getCurrentTimestamp() + " - ERROR: Appium Driver Service has not been initialized.");
            return false;
        }

        boolean isRunning = appiumDriverLocalService.isRunning();
        System.out.println(getCurrentTimestamp() + " - INFO: Appium Server running status: " + (isRunning ? "Running" : "Not Running"));
        return isRunning;
    }

    private String getCurrentTimestamp() {
        return LocalDateTime.now().format(DATE_FORMATTER);
    }
}

Link To Appium Logs

https://gist.github.com/dipakkumar1225/57756cacee02a6b124d8aeba8129f29b

@mykola-mokhnach
Copy link
Contributor

This exception usually means that stdout or stderr stream has been closed unexpectedly before the server process has been closed. Generally it means there is some race condition while closing the process.

From the server perspective the exception could be safely ignored, it does not affect much. You'd probably just not see some final server log lines as a result of this exception.

@mykola-mokhnach
Copy link
Contributor

Also the main logic responsible for process and streams handling is at the Selenium lib side. There is not much we may change there from this library's perspective.

@dipakkumar1225
Copy link
Author

@mykola-mokhnach Thank you for the reply.

on console side why no such error observed? Also any other possible way to capture the log in to file?

@mykola-mokhnach
Copy link
Contributor

Ofc there are, but only if you use a different method to run the server (not the AppiumDriverLocalService class).

I have created an issue report to Selenium about the same.

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

2 participants