# Buggy code
def _setup_pailgun(self):
    """Sets up a PailgunServer instance."""

    # Constructs and returns a runnable PantsRunner.
    def runner_factory(sock, arguments, environment):
        exiter = self._exiter_class(sock)
        build_graph = None

        self._logger.debug("execution commandline: %s", arguments)
        if self._scheduler_service:
            # N.B. This parses sys.argv by way of OptionsInitializer/OptionsBootstrapper prior to
            # the main pants run to derive spec_roots for caching in the underlying scheduler.
            spec_roots = self._parse_commandline_to_spec_roots(args=arguments)
            self._logger.debug("parsed spec_roots: %s", spec_roots)
            try:
                self._logger.debug(
                    "requesting BuildGraph from %s", self._scheduler_service
                )
                # N.B. This call is made in the pre-fork daemon context for reach and reuse of the
                # resident scheduler for BuildGraph construction.
                build_graph = self._scheduler_service.get_build_graph(spec_roots)
            except Exception:
                self._logger.warning(
                    "encountered exception during SchedulerService.get_build_graph():\n%s",
                    traceback.format_exc(),
                )
        return self._runner_class(sock, exiter, arguments, environment, build_graph)

    return PailgunServer(self._bind_addr, runner_factory)


# Traceback (AttributeError)
W0602 21:35:21.260252 22326 pailgun_service.py:69] encountered exception during SchedulerService.get_build_graph():
Traceback (most recent call last):
File "/home/travis/build/pantsbuild/pants/src/python/pants/pantsd/service/pailgun_service.py", line 65, in runner_factory
build_graph = self._scheduler_service.get_build_graph(spec_roots)
File "/home/travis/build/pantsbuild/pants/src/python/pants/pantsd/service/scheduler_service.py", line 84, in get_build_graph
return self._graph_helper.create_graph(spec_roots)
File "/home/travis/build/pantsbuild/pants/src/python/pants/bin/engine_initializer.py", line 64, in create_graph
for _ in graph.inject_specs_closure(spec_roots):  # Ensure the entire generator is unrolled.
File "/home/travis/build/pantsbuild/pants/src/python/pants/engine/legacy/graph.py", line 169, in inject_specs_closure
for address in self._inject(specs):
File "/home/travis/build/pantsbuild/pants/src/python/pants/engine/legacy/graph.py", line 185, in _inject
entries = maybe_list(state.value, expected_type=LegacyTarget)
AttributeError: 'NoneType' object has no attribute 'value'

# Fixed code
def _setup_pailgun(self):
    """Sets up a PailgunServer instance."""

    # Constructs and returns a runnable PantsRunner.
    def runner_factory(sock, arguments, environment):
        exiter = self._exiter_class(sock)
        build_graph = None

        self._logger.debug("execution commandline: %s", arguments)
        if self._scheduler_service:
            # N.B. This parses sys.argv by way of OptionsInitializer/OptionsBootstrapper prior to
            # the main pants run to derive spec_roots for caching in the underlying scheduler.
            spec_roots = self._parse_commandline_to_spec_roots(args=arguments)
            self._logger.debug("parsed spec_roots: %s", spec_roots)
            try:
                self._logger.debug(
                    "requesting BuildGraph from %s", self._scheduler_service
                )
                # N.B. This call is made in the pre-fork daemon context for reach and reuse of the
                # resident scheduler for BuildGraph construction.
                build_graph = self._scheduler_service.get_build_graph(spec_roots)
            except Exception:
                self._logger.warning(
                    "encountered exception during SchedulerService.get_build_graph():\n%s",
                    traceback.format_exc(),
                )
        return self._runner_class(sock, exiter, arguments, environment, build_graph)

    @contextmanager
    def context_lock():
        """This lock is used to safeguard Pailgun request handling against a fork() with the
        scheduler lock held by another thread (e.g. the FSEventService thread), which can
        lead to a pailgun deadlock.
        """
        if self._scheduler_service:
            with self._scheduler_service.locked():
                yield
        else:
            yield

    return PailgunServer(self._bind_addr, runner_factory, context_lock)