# Buggy code
def detect_phantomjs(version: str = "2.1") -> str:
    """Detect if PhantomJS is avaiable in PATH, at a minimum version.

    Args:
        version (str, optional) :
            Required minimum version for PhantomJS (mostly for testing)

    Returns:
        str, path to PhantomJS

    """
    if settings.phantomjs_path() is not None:
        phantomjs_path = settings.phantomjs_path()
    else:
        phantomjs_path = shutil.which("phantomjs") or "phantomjs"

    try:
        proc = Popen([phantomjs_path, "--version"], stdout=PIPE, stderr=PIPE)
        proc.wait()
        out = proc.communicate()

        if len(out[1]) > 0:
            raise RuntimeError(
                "Error encountered in PhantomJS detection: %r" % out[1].decode("utf8")
            )

        required = V(version)
        installed = V(out[0].decode("utf8"))
        if installed < required:
            raise RuntimeError(
                "PhantomJS version to old. Version>=%s required, installed: %s"
                % (required, installed)
            )

    except OSError:
        raise RuntimeError(
            'PhantomJS is not present in PATH or BOKEH_PHANTOMJS_PATH. Try "conda install phantomjs" or \
            "npm install -g phantomjs-prebuilt"'
        )

    return phantomjs_path


# Traceback (RuntimeError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/io/export.py", line 97, in export_png
image = get_screenshot_as_png(obj, height=height, width=width, driver=webdriver, timeout=timeout)
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/io/export.py", line 217, in get_screenshot_as_png
web_driver = driver if driver is not None else webdriver_control.get()
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/io/webdriver.py", line 116, in get
self.current = self.create()
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/io/webdriver.py", line 121, in create
return create_phantomjs_webdriver()
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/io/webdriver.py", line 75, in create_phantomjs_webdriver
phantomjs_path = detect_phantomjs()
File "/home/savalek/.local/lib/python3.6/site-packages/bokeh/util/dependencies.py", line 117, in detect_phantomjs
raise RuntimeError('Error encountered in PhantomJS detection: %r' % out[1].decode('utf8'))
RuntimeError: Error encountered in PhantomJS detection: "events.js:183\n      throw er; // Unhandled 'error' event\n      ^\n\nError: read ECONNRESET\n    at _errnoException (util.js:1022:11)\n    at Pipe.onread (net.js:628:25)\n"

# Fixed code
def detect_phantomjs(version: str = "2.1") -> str:
    """Detect if PhantomJS is avaiable in PATH, at a minimum version.

    Args:
        version (str, optional) :
            Required minimum version for PhantomJS (mostly for testing)

    Returns:
        str, path to PhantomJS

    """
    if settings.phantomjs_path() is not None:
        phantomjs_path = settings.phantomjs_path()
    else:
        phantomjs_path = shutil.which("phantomjs") or "phantomjs"

    try:
        proc = Popen(
            [phantomjs_path, "--version"], stdout=PIPE, stderr=PIPE, stdin=PIPE
        )
        proc.wait()
        out = proc.communicate()

        if len(out[1]) > 0:
            raise RuntimeError(
                "Error encountered in PhantomJS detection: %r" % out[1].decode("utf8")
            )

        required = V(version)
        installed = V(out[0].decode("utf8"))
        if installed < required:
            raise RuntimeError(
                "PhantomJS version to old. Version>=%s required, installed: %s"
                % (required, installed)
            )

    except OSError:
        raise RuntimeError(
            'PhantomJS is not present in PATH or BOKEH_PHANTOMJS_PATH. Try "conda install phantomjs" or \
            "npm install -g phantomjs-prebuilt"'
        )

    return phantomjs_path