ulixee / hero

The web browser built for scraping
MIT License
649 stars 32 forks source link

Hero fails to run multiple times when using headful mode (Apple Silicon) #218

Closed soundofspace closed 1 year ago

soundofspace commented 1 year ago

When creating a second hero instance after the first one closed in headful mode, hero fails to create a new chrome window. Ideally it would reuse the already existing window, and only create new ones if needed. But if a new window is needed that's also not that bad. Right now the only way to use headful mode is by restarting the node process after every hero exec. Can be reproduced with this example (can probably be easier, but leaving it here as is):

import Hero, {
  ConnectionToHeroCore,
} from '@ulixee/hero';
import HeroCore from '@ulixee/hero-core';
import TransportBridge from '@ulixee/net/lib/TransportBridge';

async function start() {
  console.log('starting core')
  await HeroCore.start({
      maxConcurrentClientCount: 5,
      shouldShutdownOnSignals: false,
  }).then(() => {
      console.log('core started')
  }).catch(() => {
      console.log('core failed to start')
  });
}

async function exec() {
  const bridge = new TransportBridge();
  const connection = new ConnectionToHeroCore(bridge.transportToCore, {
      maxConcurrency: 5,
  });
  HeroCore.addConnection(bridge.transportToClient);
  console.log(`running script`)
  const hero = new Hero({connectionToCore: connection, showChrome: true});
  await hero.goto('https://www.google.com');
  console.log('webpage loaded')
  await hero.waitForMillis(2000);
  await hero.close();
  console.log('script finished')
}

async function loop() {
  for (let i = 0; i<=100; i++) {
      console.log(`Running iteration ${i}`)
      try {
          await exec()
      } catch {
          console.log('ignored error')
      }
  }
}

start().then(() => {
  loop()
})
soundofspace commented 1 year ago

Updated title to reflect that this is an Apple Silicon problem. Can't be reproduced on older macs using Intel chips, will try on Linux in the near future.

blakebyrnes commented 1 year ago

Fixed in next release