Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alter closing sessions inside a stored procedure to write a warning instead of throwing an error #1263

Merged
merged 18 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ def _generate_new_action_id(self) -> int:
def close(self) -> None:
"""Close this session."""
if is_in_stored_procedure():
raise SnowparkClientExceptionMessages.DONT_CLOSE_SESSION_IN_SP()
_logger.warning("Closing a session in a stored procedure is a no-op.")
zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
return
try:
if self._conn.is_closed():
_logger.debug(
Expand Down
2 changes: 1 addition & 1 deletion tests/integ/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def test_use_secondary_roles(session):
session.use_secondary_roles(current_role[1:-1])


@pytest.mark.skipif(IS_IN_STORED_PROC, reason="SP doesn't allow to close a session.")
@pytest.mark.skipif(IS_IN_STORED_PROC, reason="Can't create a session in SP")
zaramzamzam marked this conversation as resolved.
Show resolved Hide resolved
def test_close_session_twice(db_parameters):
new_session = Session.builder.configs(db_parameters).create()
new_session.close()
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2012-2023 Snowflake Computing Inc. All rights reserved.
#
import json
import logging
import os
from typing import Optional
from unittest import mock
Expand Down Expand Up @@ -122,6 +123,47 @@ def test_close_exception():
session.close()


def test_close_session_in_stored_procedure_no_op():
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
fake_connection.is_closed = MagicMock(return_value=False)
session = Session(fake_connection)
with mock.patch.object(
snowflake.snowpark.session, "is_in_stored_procedure"
) as mock_fn, mock.patch.object(
session._conn, "close"
) as mock_close, mock.patch.object(
session, "cancel_all"
) as mock_cancel_all, mock.patch.object(
snowflake.snowpark.session, "_remove_session"
) as mock_remove:
mock_fn.return_value = True
session.close()
mock_cancel_all.assert_not_called()
mock_close.assert_not_called()
mock_remove.assert_not_called()


@pytest.mark.parametrize(
"warning_level, expected",
[(logging.WARNING, True), (logging.INFO, True), (logging.ERROR, False)],
)
def test_close_session_in_stored_procedure_log_level(caplog, warning_level, expected):
caplog.clear()
caplog.set_level(warning_level)
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
fake_connection.is_closed = MagicMock(return_value=False)
session = Session(fake_connection)
with mock.patch.object(
snowflake.snowpark.session, "is_in_stored_procedure"
) as mock_fn:
mock_fn.return_value = True
session.close()
result = "Closing a session in a stored procedure is a no-op." in caplog.text
assert result == expected


def test_resolve_import_path_ignore_import_path(tmp_path_factory):
fake_connection = mock.create_autospec(ServerConnection)
fake_connection._conn = mock.Mock()
Expand Down
Loading