# Buggy code
def start_kernel_from_session(
    self, kernel_id, kernel_name, connection_info, process_info, launch_args
):
    # Create a KernelManger instance and load connection and process info, then confirm the kernel is still
    # alive.
    constructor_kwargs = {}
    if self.kernel_spec_manager:
        constructor_kwargs["kernel_spec_manager"] = self.kernel_spec_manager

    # Construct a kernel manager...
    km = self.kernel_manager_factory(
        connection_file=os.path.join(self.connection_dir, "kernel-%s.json" % kernel_id),
        parent=self,
        log=self.log,
        kernel_name=kernel_name,
        **constructor_kwargs,
    )

    km.load_connection_info(connection_info)
    km.write_connection_file()

    km._launch_args = launch_args

    # Construct a process-proxy
    if km.kernel_spec.process_proxy_class:
        process_proxy_class = import_item(km.kernel_spec.process_proxy_class)
        kw = {"env": {}}
        km.process_proxy = process_proxy_class(
            km, km.kernel_spec.process_proxy_connection_file_mode, **kw
        )
        km.process_proxy.load_process_info(process_info)

        # Confirm we can even poll the process.  If not, remove the persisted session.
        if km.process_proxy.poll() is False:
            return False

    km.kernel = km.process_proxy
    km.start_restarter()
    km._connect_control_socket()
    self._kernels[kernel_id] = km
    self._kernel_connections[kernel_id] = 0
    self.start_watching_activity(kernel_id)
    self.add_restart_callback(
        kernel_id,
        lambda: self._handle_kernel_died(kernel_id),
        "dead",
    )
    # Only initialize culling if available.  Warning message will be issued in gatewayapp at startup.
    func = getattr(self, "initialize_culler", None)
    if func:
        func()
    return True


# Traceback (IOError)
[D 2017-07-16 15:56:15.197 KernelGatewayApp] RemoteKernelManager: Writing connection file with ip=169.45.103.144, control=33001, hb=45662, iopub=44204, stdin=33960, shell=44543
Traceback (most recent call last):
File "/opt/anaconda2/bin/jupyter-kernelgateway", line 11, in <module>
sys.exit(launch_instance())
File "/opt/anaconda2/lib/python2.7/site-packages/jupyter_core/application.py", line 267, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "/opt/anaconda2/lib/python2.7/site-packages/traitlets/config/application.py", line 657, in launch_instance
app.initialize(argv)
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/gatewayapp.py", line 352, in initialize
self.init_configurables()
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/gatewayapp.py", line 402, in init_configurables
self.kernel_session_manager.start_sessions()
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/sessions/kernelsessionmanager.py", line 84, in start_sessions
if not self._start_session(kernel_session):
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/sessions/kernelsessionmanager.py", line 97, in _start_session
launch_args=kernel_session['launch_args'])
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/remotemanager.py", line 46, in start_kernel_from_session
km.write_connection_file()
File "/opt/anaconda2/lib/python2.7/site-packages/kernel_gateway/services/kernels/remotemanager.py", line 143, in write_connection_file
return super(RemoteKernelManager, self).write_connection_file()
File "/opt/anaconda2/lib/python2.7/site-packages/jupyter_client/connect.py", line 431, in write_connection_file
kernel_name=self.kernel_name
File "/opt/anaconda2/lib/python2.7/site-packages/jupyter_client/connect.py", line 136, in write_connection_file
with open(fname, 'w') as f:
IOError: [Errno 13] Permission denied: u'/var/run/elyra/runtime/kernel-7388b059-8a68-48a0-811c-2d9803f04250.json'

# Fixed code
def start_kernel_from_session(
    self, kernel_id, kernel_name, connection_info, process_info, launch_args
):
    # Create a KernelManger instance and load connection and process info, then confirm the kernel is still
    # alive.
    constructor_kwargs = {}
    if self.kernel_spec_manager:
        constructor_kwargs["kernel_spec_manager"] = self.kernel_spec_manager

    # Construct a kernel manager...
    km = self.kernel_manager_factory(
        connection_file=os.path.join(self.connection_dir, "kernel-%s.json" % kernel_id),
        parent=self,
        log=self.log,
        kernel_name=kernel_name,
        **constructor_kwargs,
    )

    # Load connection info into member vars - no need to write out connection file
    km.load_connection_info(connection_info)

    km._launch_args = launch_args

    # Construct a process-proxy
    if km.kernel_spec.process_proxy_class:
        process_proxy_class = import_item(km.kernel_spec.process_proxy_class)
        kw = {"env": {}}
        km.process_proxy = process_proxy_class(
            km, km.kernel_spec.process_proxy_connection_file_mode, **kw
        )
        km.process_proxy.load_process_info(process_info)

        # Confirm we can even poll the process.  If not, remove the persisted session.
        if km.process_proxy.poll() is False:
            return False

    km.kernel = km.process_proxy
    km.start_restarter()
    km._connect_control_socket()
    self._kernels[kernel_id] = km
    self._kernel_connections[kernel_id] = 0
    self.start_watching_activity(kernel_id)
    self.add_restart_callback(
        kernel_id,
        lambda: self._handle_kernel_died(kernel_id),
        "dead",
    )
    # Only initialize culling if available.  Warning message will be issued in gatewayapp at startup.
    func = getattr(self, "initialize_culler", None)
    if func:
        func()
    return True