Repository: microsoft/playwright
File: packages/playwright/src/mcp/terminal/daemon.ts (TypeScript)

Code before:
  // Clean up existing socket file on Unix
  if (os.platform() !== 'win32' && await socketExists(socketPath)) {
    daemonDebug(`Socket already exists, removing: ${socketPath}`);
    try {
      await fs.unlink(socketPath);
    } catch (error) {
      daemonDebug(`Failed to remove existing socket: ${error}`);
      throw error;
    }
  }

  const backend = serverBackendFactory.create();
  const cwd = url.pathToFileURL(process.cwd()).href;
  await backend.initialize?.({
    name: 'playwright-cli',
    version: '1.0.0',
    roots: [{
      uri: cwd,
      name: 'cwd'
    }],
    timestamp: Date.now(),
  });

  await fs.mkdir(path.dirname(socketPath), { recursive: true });

  const server = net.createServer(socket => {
    daemonDebug('new client connection');
    const connection = new SocketConnection(socket, daemonVersion);
    connection.onclose = () => {
      daemonDebug('client disconnected');
    };
    connection.onversionerror = (id, e) => {
      if (daemonVersion === 'undefined-for-test')
        return false;

      if (semverGreater(daemonVersion, e.received)) {
        // eslint-disable-next-line no-console
        connection.send({ id, error: `Client is too old: daemon is ${daemonVersion}, client is ${e.received}.` }).catch(e => console.error(e));
      } else {
        gracefullyProcessExitDoNotHang(0, async () => {
          // eslint-disable-next-line no-console
          await connection.send({ id, error: `Daemon is too old: daemon is ${daemonVersion}, client is ${e.received}. Stopping it.` }).catch(e => console.error(e));
          server.close();
        });
      }
      return true;
    };
    connection.onmessage = async message => {
      const { id, method, params } = message;
      try {
        daemonDebug('received command', method);

Review comment (bug):
```suggestion
  let browserLaunchFailed = false;
```

Code after:
  // Clean up existing socket file on Unix
  if (os.platform() !== 'win32' && await socketExists(socketPath)) {
    daemonDebug(`Socket already exists, removing: ${socketPath}`);
    try {
      await fs.unlink(socketPath);
    } catch (error) {
      daemonDebug(`Failed to remove existing socket: ${error}`);
      throw error;
    }
  }

  const backend = serverBackendFactory.create();
  const cwd = url.pathToFileURL(process.cwd()).href;
  await backend.initialize?.({
    name: 'playwright-cli',
    version: '1.0.0',
    roots: [{
      uri: cwd,
      name: 'cwd'
    }],
    timestamp: Date.now(),
  });

  await fs.mkdir(path.dirname(socketPath), { recursive: true });

  let shutdownPending = false;

  const shutdown = (exitCode: number) => {
    daemonDebug(`shutting down daemon with exit code ${exitCode}`);
    server.close();
    gracefullyProcessExitDoNotHang(exitCode);
  };

  const server = net.createServer(socket => {
    daemonDebug('new client connection');
    const connection = new SocketConnection(socket, daemonVersion);
    connection.onclose = () => {
      daemonDebug('client disconnected');
    };
    connection.onversionerror = (id, e) => {
      if (daemonVersion === 'undefined-for-test')
        return false;

      if (semverGreater(daemonVersion, e.received)) {
        // eslint-disable-next-line no-console
        connection.send({ id, error: `Client is too old: daemon is ${daemonVersion}, client is ${e.received}.` }).catch(e => console.error(e));
      } else {
        gracefullyProcessExitDoNotHang(0, async () => {
          // eslint-disable-next-line no-console
          await connection.send({ id, error: `Daemon is too old: daemon is ${daemonVersion}, client is ${e.received}. Stopping it.` }).catch(e => console.error(e));
          server.close();