You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux服务器下Selenium启动多Chrome窗口崩溃原因排查

排查Selenium启动多Chrome窗口崩溃的后端限制因素

我要排查用Python Selenium WebDriver启动多个独立Chrome窗口(每个窗口仅含1个空白标签)时的崩溃触发条件,因此运行循环代码直至程序崩溃,出现了以下3种错误,想了解导致Chrome崩溃的后端限制因素,是否是内存、线程或进程限制触发了崩溃?

测试代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')

flag = 1
while flag:
    try:
        driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
    except:
        flag = 0

出现的错误

错误1

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

错误2

File "/../../python/current/lib/python2.7/subprocess.py", line 1231, in _execute_child
    self.pid = os.fork()
OSError: [Errno 11] Resource temporarily unavailable

错误3

selenium.common.exceptions.WebDriverException: Message: session not created
from tab crashed
  (Session info: headless chrome=97.0.4692.71)

后端限制因素分析

这些错误全部源于系统资源耗尽,不同错误对应不同的资源瓶颈:

  1. 错误2直接指向进程数限制
    Linux系统对每个用户有进程数上限(可通过ulimit -u查看当前配额)。你的代码循环创建Chrome实例但从不调用driver.quit()销毁进程,每个Chrome实例包含主进程、渲染进程等多个子进程,很快就会耗尽用户进程配额,导致Python的subprocess模块无法通过os.fork()创建新进程,抛出Resource temporarily unavailable错误。

  2. 错误1和错误3源于资源耗尽的连锁反应

    • 内存耗尽:即使是headless模式的空白标签页,每个Chrome实例也会占用约50-100MB内存。大量实例持续运行会吃光系统内存,Chrome启动时无法分配足够内存,进程直接崩溃,导致DevToolsActivePort文件未生成(错误1),或会话创建时渲染进程无法启动(错误3)。
    • 文件描述符限制:每个进程会占用文件描述符(用于文件、套接字等),系统和用户级的文件描述符上限耗尽后,Chrome无法创建必要的通信套接字或临时文件,引发启动失败或标签崩溃。
    • 进程数限制的间接影响:即使未触发fork报错,剩余进程配额不足时,Chrome内部的渲染进程、GPU进程等子进程无法创建,也会导致主进程崩溃,出现错误1或错误3的提示。

补充:你的代码未销毁任何Chrome实例,是加速资源耗尽的核心原因,正常场景下必须在使用完后调用driver.quit()释放资源。

内容的提问来源于stack exchange,提问作者Dinesh Khanna V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 21:05:19