# Buggy code
def handle_response(self, response: "Optional[List[Dict]]", pos) -> None:
    window = self.view.window()

    if response is None:
        response = []

    references_count = len(response)
    # return if there are no references
    if references_count < 1:
        window.run_command("hide_panel", {"panel": "output.references"})
        window.status_message("No references found")
        return

    word_region = self.view.word(pos)
    word = self.view.substr(word_region)

    base_dir = get_project_path(window)
    formatted_references = self._get_formatted_references(response, base_dir)

    if settings.show_references_in_quick_panel:
        flags = sublime.KEEP_OPEN_ON_FOCUS_LOST
        if settings.quick_panel_monospace_font:
            flags |= sublime.MONOSPACE_FONT
        window.show_quick_panel(
            self.reflist,
            lambda index: self.on_ref_choice(base_dir, index),
            flags,
            self.get_current_ref(base_dir, word_region.begin()),
            lambda index: self.on_ref_highlight(base_dir, index),
        )
    else:
        panel = ensure_references_panel(window)
        if not panel:
            return
        panel.settings().set("result_base_dir", base_dir)

        panel.set_read_only(False)
        panel.run_command("lsp_clear_panel")
        window.run_command("show_panel", {"panel": "output.references"})
        panel.run_command(
            "append",
            {
                "characters": "{} references for '{}'\n\n{}".format(
                    references_count, word, formatted_references
                ),
                "force": True,
                "scroll_to_end": False,
            },
        )

        # highlight all word occurrences
        regions = panel.find_all(r"\b{}\b".format(word))
        panel.add_regions(
            "ReferenceHighlight", regions, "comment", flags=sublime.DRAW_OUTLINED
        )
        panel.set_read_only(True)


# Traceback (BrokenPipeError)
startup, version: 3207 osx x64 channel: stable
executable: /Applications/Sublime Text.app/Contents/MacOS/Sublime Text
working dir: /
packages path: /Users/perm/Library/Application Support/Sublime Text 3/Packages
state path: /Users/perm/Library/Application Support/Sublime Text 3/Local
zip path: /Applications/Sublime Text.app/Contents/MacOS/Packages
zip path: /Users/perm/Library/Application Support/Sublime Text 3/Installed Packages
ignored_packages: ["Vintage"]
pre session restore time: 0.181761
using gpu buffer for window
startup time: 0.258246
environment variables loaded using: /bin/bash -l
reloading plugin Default.arithmetic
reloading plugin Default.auto_indent_tag
reloading plugin Default.block
reloading plugin Default.colors
reloading plugin Default.comment
reloading plugin Default.convert_color_scheme
reloading plugin Default.convert_syntax
reloading plugin Default.copy_path
reloading plugin Default.detect_indentation
reloading plugin Default.echo
reloading plugin Default.exec
reloading plugin Default.fold
reloading plugin Default.font
reloading plugin Default.goto_line
reloading plugin Default.history_list
reloading plugin Default.indentation
reloading plugin Default.install_package_control
reloading plugin Default.kill_ring
reloading plugin Default.mark
reloading plugin Default.new_templates
reloading plugin Default.open_context_url
reloading plugin Default.open_in_browser
reloading plugin Default.pane
reloading plugin Default.paragraph
reloading plugin Default.paste_from_history
reloading plugin Default.profile
reloading plugin Default.quick_panel
reloading plugin Default.rename
reloading plugin Default.run_syntax_tests
reloading plugin Default.save_on_focus_lost
reloading plugin Default.scroll
reloading plugin Default.set_unsaved_view_name
reloading plugin Default.settings
reloading plugin Default.show_scope_name
reloading plugin Default.side_bar
reloading plugin Default.sort
reloading plugin Default.switch_file
reloading plugin Default.symbol
reloading plugin Default.transform
reloading plugin Default.transpose
reloading plugin Default.ui
reloading plugin CSS.css_completions
reloading plugin Diff.diff
reloading plugin HTML.encode_html_entities
reloading plugin HTML.html_completions
reloading plugin ShellScript.ShellScript
reloading plugin 0_package_control_loader.00-package_control
reloading plugin 0_package_control_loader.01-pygments
reloading plugin 0_package_control_loader.50-markupsafe
reloading plugin 0_package_control_loader.50-pymdownx
reloading plugin 0_package_control_loader.50-python-markdown
reloading plugin 0_package_control_loader.50-pyyaml
reloading plugin 0_package_control_loader.51-python-jinja2
reloading plugin 0_package_control_loader.55-mdpopups
reloading plugin LSP.boot
reloading plugin Package Control.1_reloader
reloading plugin Package Control.2_bootstrap
reloading plugin Package Control.Package Control
plugins loaded
LSP: global configs ['jdtls=False', 'phpls=False', 'flow=False', 'reason=False', 'cquery=False', 'clangd=False', 'lsp-tsserver=False', 'eslint=False', 'ocaml=False', 'ruby=False', 'javascript-typescript-langserver=False', 'pyls=False', 'haskell-ide-engine=False', 'ra-lsp=False', 'polymer-ide=False', 'gopls=False', 'vscode-css=False', 'intelephense-ls=False', 'metals=False', 'typescript-language-server=False', 'golsp=False', 'dart=False', 'spider-nc=True', 'rls=False', 'bashls=False']
LSP: Couldn't determine project directory since no folders are open and the current file isn't saved on the disk.
LSP: window 2 starting 1 initial views
LSP: window 2 requests spider-nc for /Users/perm/sicstus/lsptestfolder/foo.pl
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/sicstus/lsptestfolder as a fallback.
LSP: starting in /Users/perm/sicstus/lsptestfolder
LSP: starting ['/usr/bin/nc', 'localhost', '56789']
LSP:  --> initialize
LSP: window 2 added session spider-nc
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/sicstus/lsptestfolder as a fallback.
LSP: project path changed, ending existing sessions
LSP: new path = /Users/perm/sicstus/lsptestfolder
LSP: unloading session spider-nc
LSP:  --> shutdown
Package Control: Skipping automatic upgrade, last run at 2019-08-01 16:21:54, next run at 2019-08-01 17:21:54 or after
LSP:      {'capabilities': {'documentFormattingProvider': True, 'implementationProvider': False, 'codeActionProvider': False, 'callHierarchyProvider': False, 'renameProvider': True, 'typeDefinitionProvider': False, 'workspaceSymbolProvider': True, 'definitionProvider': True, 'documentRangeFormattingProvider': True, 'workspace': {'workspaceFolders': {'supported': True, 'changeNotifications': True}}, 'typeHierarchyProvider': False, 'documentHighlightProvider': True, 'referencesProvider': True, 'textDocumentSync': 2, 'hoverProvider': True, 'documentSymbolProvider': True, 'completionProvider': {'triggerCharacters': ['(']}}}
LSP:  --> initialized
LSP:  --> textDocument/didOpen
LSP:      None
LSP:  --> exit
LSP: session spider-nc ended
LSP: clients for window 2 unloaded
LSP: window 2 requests spider-nc for /Users/perm/sicstus/lsptestfolder/foo.pl
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/sicstus/lsptestfolder as a fallback.
LSP: starting in /Users/perm/sicstus/lsptestfolder
LSP: starting ['/usr/bin/nc', 'localhost', '56789']
LSP:  --> initialize
LSP: window 2 added session spider-nc
LSP: LSP stdout process ended.
using gpu buffer for window
Unable to open /Users/perm/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/Library/Application Support/Sublime Text 3/Packages/User as a fallback.
LSP: window 3 requests spider-nc for /Users/perm/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/Library/Application Support/Sublime Text 3/Packages/User as a fallback.
LSP: starting in /Users/perm/Library/Application Support/Sublime Text 3/Packages/User
LSP: starting ['/usr/bin/nc', 'localhost', '56789']
LSP:  --> initialize
LSP: window 3 added session spider-nc
LSP: LSP stdout process ended.
LSP: no config found or enabled for view /Users/perm/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings
LSP: no config found or enabled for view /Users/perm/Library/Application Support/Sublime Text 3/Packages/Default/Preferences.sublime-settings
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/Library/Application Support/Sublime Text 3/Packages/User as a fallback.
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/Library/Application Support/Sublime Text 3/Packages/User as a fallback.
LSP: Couldn't determine project directory since no folders are open and the current file isn't saved on the disk.
LSP: project path changed, ending existing sessions
LSP: new path = None
LSP: unloading session spider-nc
LSP:  --> shutdown
Failure writing to stdout
Traceback (most recent call last):
File "/Users/perm/Library/Application Support/Sublime Text 3/Installed Packages/LSP.sublime-package/plugin/core/transports.py", line 227, in write_stdin
self.process.stdin.flush()
BrokenPipeError: [Errno 32] Broken pipe

LSP: Communication to server closed, exiting
LSP: transport failed
LSP: Couldn't determine project directory since no folders are open! Using /Users/perm/sicstus/lsptestfolder as a fallback.
reloading settings Packages/User/Preferences.sublime-settings
reloading settings Packages/User/Preferences.sublime-settings
reloading settings Packages/User/Preferences.sublime-settings
reloading settings Packages/User/Preferences.sublime-settings

# Fixed code
def handle_response(self, response: "Optional[List[Dict]]", pos) -> None:
    window = self.view.window()

    if response is None:
        response = []

    references_count = len(response)
    # return if there are no references
    if references_count < 1:
        window.run_command("hide_panel", {"panel": "output.references"})
        window.status_message("No references found")
        return

    word_region = self.view.word(pos)
    word = self.view.substr(word_region)

    base_dir = windows.lookup(window).get_project_path()
    formatted_references = self._get_formatted_references(response, base_dir)

    if settings.show_references_in_quick_panel:
        flags = sublime.KEEP_OPEN_ON_FOCUS_LOST
        if settings.quick_panel_monospace_font:
            flags |= sublime.MONOSPACE_FONT
        window.show_quick_panel(
            self.reflist,
            lambda index: self.on_ref_choice(base_dir, index),
            flags,
            self.get_current_ref(base_dir, word_region.begin()),
            lambda index: self.on_ref_highlight(base_dir, index),
        )
    else:
        panel = ensure_references_panel(window)
        if not panel:
            return
        panel.settings().set("result_base_dir", base_dir)

        panel.set_read_only(False)
        panel.run_command("lsp_clear_panel")
        window.run_command("show_panel", {"panel": "output.references"})
        panel.run_command(
            "append",
            {
                "characters": "{} references for '{}'\n\n{}".format(
                    references_count, word, formatted_references
                ),
                "force": True,
                "scroll_to_end": False,
            },
        )

        # highlight all word occurrences
        regions = panel.find_all(r"\b{}\b".format(word))
        panel.add_regions(
            "ReferenceHighlight", regions, "comment", flags=sublime.DRAW_OUTLINED
        )
        panel.set_read_only(True)