# Buggy code
def __init__(self, kernel_manager, connection_file_mode, **kw):
    self.kernel_manager = kernel_manager
    # use the zero-ip from the start, can prevent having to write out connection file again
    self.kernel_manager.ip = "0.0.0.0"

    self.connection_file_mode = connection_file_mode
    if self.connection_file_mode:
        if self.connection_file_mode not in connection_file_modes:
            self.log.warning(
                "Unknown connection file mode detected '{}'!  Continuing...".format(
                    self.connection_file_mode
                )
            )

    self.log = kernel_manager.log
    # extract the kernel_id string from the connection file and set the KERNEL_ID environment variable
    self.kernel_id = (
        os.path.basename(self.kernel_manager.connection_file)
        .replace("kernel-", "")
        .replace(".json", "")
    )

    # ask the subclass for the set of applicable hosts
    self.hosts = self.get_hosts()

    env_dict = kw["env"]
    # see if KERNEL_LAUNCH_TIMEOUT was included from user
    self.kernel_launch_timeout = float(
        env_dict.get("KERNEL_LAUNCH_TIMEOUT", elyra_kernel_launch_timeout)
    )

    # add the applicable kernel_id to the env dict
    env_dict["KERNEL_ID"] = self.kernel_id
    for k in env_pop_list:
        env_dict.pop(k, None)
    self.log.debug("BaseProcessProxy env: {}".format(kw["env"]))

    # Represents the local process (from popen) if applicable.  Note that we could have local_proc = None even when
    # the subclass is a LocalProcessProxy (or YarnProcessProxy).  This will happen if the JKG is restarted and the
    # persisted kernel-sessions indicate that its now running on a different server.  In those case, we use the ip
    # member variable to determine if the persisted state is local or remote and use signals with the pid to
    # implement the poll, kill and send_signal methods.
    self.local_proc = None
    self.ip = None
    self.pid = 0


# Traceback (KeyError)
[D 2017-07-26 12:02:29.586 KernelGatewayApp] Instantiating kernel 'Spark 2.1 - Scala (YARN Cluster Mode)' with process proxy: kernel_gateway.services.kernels.processproxy.YarnProcessProxy
[I 170726 12:02:29 base:37] Request http://elyra-fyi-node-1:8088//ws/v1/cluster/nodes
[E 2017-07-26 12:02:29.591 KernelGatewayApp] Unhandled error in API request
Traceback (most recent call last):
File "/opt/anaconda2/lib/python2.7/site-packages/notebook/base/handlers.py", line 521, in wrapper
result = yield gen.maybe_future(method(self, *args, **kwargs))
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1063, in run
yielded = self.gen.throw(*exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/notebook/services/kernels/handlers.py", line 49, in post
kernel_id = yield gen.maybe_future(km.start_kernel(kernel_name=model['name']))
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1063, in run
yielded = self.gen.throw(*exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/remotemanager.py", line 24, in start_kernel
kernel_id = yield gen.maybe_future(super(RemoteMappingKernelManager, self).start_kernel(*args, **kwargs))
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1063, in run
yielded = self.gen.throw(*exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/manager.py", line 81, in start_kernel
kernel_id = yield gen.maybe_future(super(SeedingMappingKernelManager, self).start_kernel(*args, **kwargs))
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "/opt/anaconda2/lib/python2.7/site-packages/tornado/gen.py", line 307, in wrapper
yielded = next(result)
File "/opt/anaconda2/lib/python2.7/site-packages/notebook/services/kernels/kernelmanager.py", line 123, in start_kernel
super(MappingKernelManager, self).start_kernel(**kwargs)
File "/opt/anaconda2/lib/python2.7/site-packages/jupyter_client/multikernelmanager.py", line 110, in start_kernel
km.start_kernel(**kwargs)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/remotemanager.py", line 91, in start_kernel
self.process_proxy = process_proxy_class(self, self.kernel_spec.process_proxy_connection_file_mode, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/processproxy.py", line 570, in __init__
super(YarnProcessProxy, self).__init__(kernel_manager, connection_file_mode, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/processproxy.py", line 283, in __init__
super(RemoteProcessProxy, self).__init__(kernel_manager, connection_file_mode, **kw)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/processproxy.py", line 69, in __init__
env_dict = kw['env']
KeyError: 'env'

# Fixed code
def __init__(self, kernel_manager, connection_file_mode, **kw):
    self.kernel_manager = kernel_manager
    # use the zero-ip from the start, can prevent having to write out connection file again
    self.kernel_manager.ip = "0.0.0.0"

    self.connection_file_mode = connection_file_mode
    if self.connection_file_mode:
        if self.connection_file_mode not in connection_file_modes:
            self.log.warning(
                "Unknown connection file mode detected '{}'!  Continuing...".format(
                    self.connection_file_mode
                )
            )

    self.log = kernel_manager.log
    # extract the kernel_id string from the connection file and set the KERNEL_ID environment variable
    self.kernel_id = (
        os.path.basename(self.kernel_manager.connection_file)
        .replace("kernel-", "")
        .replace(".json", "")
    )

    # ask the subclass for the set of applicable hosts
    self.hosts = self.get_hosts()

    # Represents the local process (from popen) if applicable.  Note that we could have local_proc = None even when
    # the subclass is a LocalProcessProxy (or YarnProcessProxy).  This will happen if the JKG is restarted and the
    # persisted kernel-sessions indicate that its now running on a different server.  In those case, we use the ip
    # member variable to determine if the persisted state is local or remote and use signals with the pid to
    # implement the poll, kill and send_signal methods.
    self.local_proc = None
    self.ip = None
    self.pid = 0