# Buggy code
def __init__(self, prespawn_count, kernel_manager):
    self.kernel_manager = kernel_manager
    # Make sure we've got a int
    if not prespawn_count:
        prespawn_count = 0
    env = dict(os.environ)
    env["KERNEL_USERNAME"] = prespawn_username
    for _ in range(prespawn_count):
        self.kernel_manager.start_seeded_kernel(env=env)


# 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, prespawn_count, kernel_manager):
    self.kernel_manager = kernel_manager
    # Make sure we've got a int
    if not prespawn_count:
        prespawn_count = 0
    env = dict(os.environ.copy())
    env["KERNEL_USERNAME"] = prespawn_username
    for _ in range(prespawn_count):
        self.kernel_manager.start_seeded_kernel(env=env)