diff --git a/remotespark/livyclientlib/livyclient.py b/remotespark/livyclientlib/livyclient.py
index 5559856..eab5eed 100644
--- a/remotespark/livyclientlib/livyclient.py
+++ b/remotespark/livyclientlib/livyclient.py
@@ -10,16 +10,15 @@ class LivyClient(object):
 
     def __init__(self, session):
         self.logger = Log("LivyClient")
-
-        execute_timeout_seconds = conf.execute_timeout_seconds()
-
         self._session = session
-        self._session.create_sql_context()
-        self._execute_timeout_seconds = execute_timeout_seconds
+        self._execute_timeout_seconds = conf.execute_timeout_seconds()
 
     def __str__(self):
         return str(self._session)
 
+    def start(self):
+        self._session.create_sql_context()
+
     def serialize(self):
         return self._session.get_state().to_dict()
 
diff --git a/remotespark/livyclientlib/sparkcontroller.py b/remotespark/livyclientlib/sparkcontroller.py
index 1254f48..fcc6ae6 100644
--- a/remotespark/livyclientlib/sparkcontroller.py
+++ b/remotespark/livyclientlib/sparkcontroller.py
@@ -72,6 +72,7 @@ class SparkController(object):
         session.start()
         livy_client = self.client_factory.build_client(session)
         self.client_manager.add_client(name, livy_client)
+        livy_client.start()
 
     def get_client_keys(self):
         return self.client_manager.get_sessions_list()

Shutdown pyspark kernel doesn't gaurantee that session has been deleted 
Repro Steps: 
- Open Pyspark kernel 
- Do any operation, 1+1 for example 
- shutdown it before getting the answer (while creating the SQL context & Hive one)
- SSH the cluster, you'll find the session still existing

For now, to delete it, you have to do that manually from ssh the cluster.
diff --git a/tests/test_livyclient.py b/tests/test_livyclient.py
index 57870fa..1a2931a 100644
--- a/tests/test_livyclient.py
+++ b/tests/test_livyclient.py
@@ -6,16 +6,23 @@ from remotespark.utils.utils import get_connection_string
 from remotespark.utils.constants import Constants
 
 
-def test_create_sql_context_automatically():
+def test_doesnt_create_sql_context_automatically():
     mock_spark_session = MagicMock()
     LivyClient(mock_spark_session)
+    assert not mock_spark_session.create_sql_context.called
 
+
+def test_start_creates_sql_context():
+    mock_spark_session = MagicMock()
+    client = LivyClient(mock_spark_session)
+    client.start()
     mock_spark_session.create_sql_context.assert_called_with()
 
 
 def test_execute_code():
     mock_spark_session = MagicMock()
     client = LivyClient(mock_spark_session)
+    client.start()
     command = "command"
 
     client.execute(command)
@@ -28,6 +35,7 @@ def test_execute_code():
 def test_execute_sql():
     mock_spark_session = MagicMock()
     client = LivyClient(mock_spark_session)
+    client.start()
     command = "command"
 
     client.execute_sql(command)
@@ -40,6 +48,7 @@ def test_execute_sql():
 def test_execute_hive():
     mock_spark_session = MagicMock()
     client = LivyClient(mock_spark_session)
+    client.start()
     command = "command"
 
     client.execute_hive(command)
@@ -63,6 +72,7 @@ def test_serialize():
     session.get_state.return_value = LivySessionState(session_id, connection_string, kind, sql_created)
 
     client = LivyClient(session)
+    client.start()
 
     serialized = client.serialize()
 
@@ -77,6 +87,7 @@ def test_serialize():
 def test_close_session():
     mock_spark_session = MagicMock()
     client = LivyClient(mock_spark_session)
+    client.start()
 
     client.close_session()
 
@@ -89,6 +100,7 @@ def test_kind():
     language_mock = PropertyMock(return_value=kind)
     type(mock_spark_session).kind = language_mock
     client = LivyClient(mock_spark_session)
+    client.start()
 
     l = client.kind
 
@@ -101,6 +113,7 @@ def test_session_id():
     session_id_mock = PropertyMock(return_value=session_id)
     type(mock_spark_session).id = session_id_mock
     client = LivyClient(mock_spark_session)
+    client.start()
 
     i = client.session_id
 
diff --git a/tests/test_sparkcontroller.py b/tests/test_sparkcontroller.py
index e522404..4aa9a12 100644
--- a/tests/test_sparkcontroller.py
+++ b/tests/test_sparkcontroller.py
@@ -41,7 +41,7 @@ def test_add_session():
     name = "name"
     properties = {"kind": "spark"}
     connection_string = "url=http://location:port;username=name;password=word"
-    client = "client"
+    client = MagicMock()
     session = MagicMock()
     client_factory.create_session = MagicMock(return_value=session)
     client_factory.build_client = MagicMock(return_value=client)
@@ -51,6 +51,7 @@ def test_add_session():
     client_factory.create_session.assert_called_once_with(connection_string, properties, "-1", False)
     client_factory.build_client.assert_called_once_with(session)
     client_manager.add_client.assert_called_once_with(name, client)
+    client.start.assert_called_once_with()
     session.start.assert_called_once_with()
 
 

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.9
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
anyio==4.9.0
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
async-lru==2.0.5
attrs==25.3.0
babel==2.17.0
beautifulsoup4==4.13.3
bleach==6.2.0
certifi==2025.1.31
cffi==1.17.1
charset-normalizer==3.4.1
comm==0.2.2
decorator==5.2.1
defusedxml==0.7.1
exceptiongroup==1.2.2
fastjsonschema==2.21.1
fqdn==1.5.1
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
importlib_metadata==8.6.1
iniconfig==2.1.0
ipykernel==4.1.1
ipython==4.0.0
ipython-genutils==0.2.0
ipywidgets==7.8.5
isoduration==20.11.0
Jinja2==3.1.6
json5==0.10.0
jsonpointer==3.0.0
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
jupyter-events==0.12.0
jupyter-lsp==2.2.5
jupyter_client==8.6.3
jupyter_core==5.7.2
jupyter_server==2.15.0
jupyter_server_terminals==0.5.3
jupyterlab==4.1.5
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.3
jupyterlab_widgets==1.1.11
MarkupSafe==3.0.2
mistune==3.1.3
mock==5.2.0
narwhals==1.32.0
nbclient==0.10.2
nbconvert==7.16.6
nbformat==5.10.4
nose==1.3.7
notebook==7.1.3
notebook_shim==0.2.4
numpy==2.0.2
overrides==7.7.0
packaging==24.2
pandas==2.2.3
pandocfilters==1.5.1
pexpect==4.9.0
pickleshare==0.7.5
platformdirs==4.3.7
plotly==6.0.1
pluggy==1.5.0
prometheus_client==0.21.1
ptyprocess==0.7.0
pycparser==2.22
Pygments==2.19.1
pytest==8.3.5
python-dateutil==2.9.0.post0
python-json-logger==3.3.0
pytz==2025.2
PyYAML==6.0.2
pyzmq==26.3.0
referencing==0.36.2
-e git+https://github.com/jupyter-incubator/sparkmagic.git@cb87d63ab3268c1c5bd63c9fc1f1e971d5a1fe31#egg=remotespark
requests==2.32.3
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rpds-py==0.24.0
Send2Trash==1.8.3
simplegeneric==0.8.1
six==1.17.0
sniffio==1.3.1
soupsieve==2.6
terminado==0.18.1
tinycss2==1.4.0
tomli==2.2.1
tornado==6.4.2
traitlets==5.14.3
types-python-dateutil==2.9.0.20241206
typing_extensions==4.13.0
tzdata==2025.2
uri-template==1.3.0
urllib3==2.3.0
webcolors==24.11.1
webencodings==0.5.1
websocket-client==1.8.0
widgetsnbextension==3.6.10
zipp==3.21.0

name: sparkmagic
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - anyio==4.9.0
      - argon2-cffi==23.1.0
      - argon2-cffi-bindings==21.2.0
      - arrow==1.3.0
      - async-lru==2.0.5
      - attrs==25.3.0
      - babel==2.17.0
      - beautifulsoup4==4.13.3
      - bleach==6.2.0
      - certifi==2025.1.31
      - cffi==1.17.1
      - charset-normalizer==3.4.1
      - comm==0.2.2
      - decorator==5.2.1
      - defusedxml==0.7.1
      - exceptiongroup==1.2.2
      - fastjsonschema==2.21.1
      - fqdn==1.5.1
      - h11==0.14.0
      - httpcore==1.0.7
      - httpx==0.28.1
      - idna==3.10
      - importlib-metadata==8.6.1
      - iniconfig==2.1.0
      - ipykernel==4.1.1
      - ipython==4.0.0
      - ipython-genutils==0.2.0
      - ipywidgets==7.8.5
      - isoduration==20.11.0
      - jinja2==3.1.6
      - json5==0.10.0
      - jsonpointer==3.0.0
      - jsonschema==4.23.0
      - jsonschema-specifications==2024.10.1
      - jupyter-client==8.6.3
      - jupyter-core==5.7.2
      - jupyter-events==0.12.0
      - jupyter-lsp==2.2.5
      - jupyter-server==2.15.0
      - jupyter-server-terminals==0.5.3
      - jupyterlab==4.1.5
      - jupyterlab-pygments==0.3.0
      - jupyterlab-server==2.27.3
      - jupyterlab-widgets==1.1.11
      - markupsafe==3.0.2
      - mistune==3.1.3
      - mock==5.2.0
      - narwhals==1.32.0
      - nbclient==0.10.2
      - nbconvert==7.16.6
      - nbformat==5.10.4
      - nose==1.3.7
      - notebook==7.1.3
      - notebook-shim==0.2.4
      - numpy==2.0.2
      - overrides==7.7.0
      - packaging==24.2
      - pandas==2.2.3
      - pandocfilters==1.5.1
      - pexpect==4.9.0
      - pickleshare==0.7.5
      - platformdirs==4.3.7
      - plotly==6.0.1
      - pluggy==1.5.0
      - prometheus-client==0.21.1
      - ptyprocess==0.7.0
      - pycparser==2.22
      - pygments==2.19.1
      - pytest==8.3.5
      - python-dateutil==2.9.0.post0
      - python-json-logger==3.3.0
      - pytz==2025.2
      - pyyaml==6.0.2
      - pyzmq==26.3.0
      - referencing==0.36.2
      - requests==2.32.3
      - rfc3339-validator==0.1.4
      - rfc3986-validator==0.1.1
      - rpds-py==0.24.0
      - send2trash==1.8.3
      - simplegeneric==0.8.1
      - six==1.17.0
      - sniffio==1.3.1
      - soupsieve==2.6
      - terminado==0.18.1
      - tinycss2==1.4.0
      - tomli==2.2.1
      - tornado==6.4.2
      - traitlets==5.14.3
      - types-python-dateutil==2.9.0.20241206
      - typing-extensions==4.13.0
      - tzdata==2025.2
      - uri-template==1.3.0
      - urllib3==2.3.0
      - webcolors==24.11.1
      - webencodings==0.5.1
      - websocket-client==1.8.0
      - widgetsnbextension==3.6.10
      - zipp==3.21.0
prefix: /opt/conda/envs/sparkmagic

tests/test_livyclient.py::test_doesnt_create_sql_context_automatically tests/test_livyclient.py::test_start_creates_sql_context tests/test_livyclient.py::test_execute_code tests/test_livyclient.py::test_execute_sql tests/test_livyclient.py::test_execute_hive tests/test_livyclient.py::test_serialize tests/test_livyclient.py::test_close_session tests/test_livyclient.py::test_kind tests/test_livyclient.py::test_session_id
tests/test_sparkcontroller.py::test_add_session tests/test_sparkcontroller.py::test_add_session_skip tests/test_sparkcontroller.py::test_delete_session tests/test_sparkcontroller.py::test_cleanup tests/test_sparkcontroller.py::test_run_cell tests/test_sparkcontroller.py::test_get_client_keys tests/test_sparkcontroller.py::test_get_all_sessions tests/test_sparkcontroller.py::test_cleanup_endpoint tests/test_sparkcontroller.py::test_delete_session_by_id_existent tests/test_sparkcontroller.py::test_delete_session_by_id_non_existent
remotespark/livyclientlib/livyclient.py remotespark/livyclientlib/sparkcontroller.py
remotespark/livyclientlib/livyclient.py remotespark/livyclientlib/sparkcontroller.py