# Buggy code
def create_session(self, kernel_id, **kwargs):
    # Persists information about the kernel session within the designated repository.

    km = self.kernel_manager.get_kernel(kernel_id)

    # Compose the kernel_session entry
    kernel_session = dict()
    kernel_session["kernel_id"] = kernel_id
    kernel_session["username"] = kwargs["env"]["KERNEL_USERNAME"]
    kernel_session["kernel_name"] = km.kernel_name

    # Build the inner dictionaries: connection_info, process_proxy and add to kernel_session
    kernel_session["connection_info"] = km.get_connection_info()
    kernel_session["launch_args"] = kwargs.copy()
    kernel_session["process_info"] = (
        km.process_proxy.get_process_info() if km.process_proxy else {}
    )
    self._save_session(kernel_id, kernel_session)


# 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 create_session(self, kernel_id, **kwargs):
    # Persists information about the kernel session within the designated repository.

    km = self.kernel_manager.get_kernel(kernel_id)

    # Compose the kernel_session entry
    kernel_session = dict()
    kernel_session["kernel_id"] = kernel_id
    kernel_session["username"] = self._get_kernel_username(kwargs.get("env", {}))
    kernel_session["kernel_name"] = km.kernel_name

    # Build the inner dictionaries: connection_info, process_proxy and add to kernel_session
    kernel_session["connection_info"] = km.get_connection_info()
    kernel_session["launch_args"] = kwargs.copy()
    kernel_session["process_info"] = (
        km.process_proxy.get_process_info() if km.process_proxy else {}
    )
    self._save_session(kernel_id, kernel_session)