Search code examples
node.jssnowflake-cloud-data-platformsnowflake-connector

Snowflake session policy with idle timeout doesn't terminate sessions. Even with KEEP_ALIVE=false


Context

I am trying to reproduce the case where a session gets closed by inactivity.

There is 'manual' way of doing it, which is to run SYSTEM$ABORT_SESSION(<session_id>), making your session no longer active.

That's ok, but I'm trying to understand how a real life scenario would work. The default idle time is 4 hours, so I tried using a session policy with a smaller timeout, therefore making it more acceptable to make some unit tests.

Steps

Created a session policy with a 5 minutes idle timeout

CREATE OR REPLACE SESSION POLICY my_session_policy
  session_idle_timeout_mins = 5
  comment = 'temporary session policy for testing purposes'

Attached it to a NEW user

CREATE USER my_newly_created_user;
ALTER USER my_newly_created_user SET disabled=true;

-- here
ALTER USER my_newly_created_user SET SESSION POLICY my_session_policy;

ALTER USER my_newly_created_user SET disabled=false;

Created a connection for my new user

const connection = snowflake.createConnection({
  account: MY_ACCOUNT,
  username: 'my_newly_created_user',
  password: NEW_USER_PASSWORD,
  database: MY_DB,
  application: someRandomName(),
  clientSessionKeepAlive: false // it's supposed to default to false anyway
})

const client = await connection.connect()

Run some query with client, just to test if the session was successfully initiated.


Then waited for more than 5 minutes (exceeded idle timeout) with no session activity, expecting the session to be automatically closed, but...

the session still works, and it is actually listed as "open" in the Snowflake Web UI.

I was expecting my Snowflake client to throw a 'terminated sessions' error.

Platform

All of above queries and code were performed on Node using the official snowflake connector at its last version (1.6.10).


There are pieces of Snowflake docs that are contradictory to what happens in my case.

A session policy defines the idle session timeout period in minutes and provides the option to override the default idle timeout value of 4 hours.

The timeout period begins upon a successful authentication to Snowflake. If a session policy is not set, Snowflake uses a default value of 240 minutes (i.e. 4 hours). The minimum configurable idle timeout value for a session policy is 5 minutes.


Conclusion

As far as I concern, my session policy should override the default idle timeout, therefore making sessions of my_newly_created_user useless after 5 minutes.

Am I missing some step to make my_newly_created_user to use my_session_policy? The docs are not very explicit regarding this. And I don't know how to debug that.


Solution

  • Can you check if the enforce_session_policy parameter is set to true?

    show parameters like 'enforce_session_policy' in account;
    

    The default value is false, so you need to set it to true like so:

    alter account set enforce_session_policy = true;
    

    Once you have changed the parameter to true, try to connect and wait 5 minutes. After 5 minutes, try to execute a query.

    You will get an error message like:

    Session no longer exists.