expresssessionnestjsexpress-session

Why does clearing all sessions make my NestJS app unresponsive?


I have a NestJS app, with a special Admin route I want to be able to call to clear out all user sessions (for example: after we deploy a new version of the application so we can kick everyone out).

I have this very basic method

import * as session from 'express-session';

@Injectable()
export class AdminService {
  constructor(private readonly sessionStore: session.Store) {}

  clearAllSessions(): string {
    try {
      this.sessionStore.clear();
      return 'All sessions cleared!';
    } catch(ex) {
      return 'Unable to clear sessions!';
    }
  }
}

Not shown here: I am using the session-file-store package and my sessions are save as files. Since express-session is agnostic to that, I wouldn't think the specifics of setting that up matter much here.

Anyway, I call the above method and it works, it clears out the sessions and I can verify they are gone. However after doing this (only tested locally so far) the app becomes unresponsive and nothing seems to load afterwards!

I assume this is partially because my own session was cleared out, but If I were to go and manually delete my own session, the next time I load the app a new session is automatically created for me as I would expect, so why does clearing all sessions cause it to behave differently and not load anything?


Solution

  • The sessionStore.clear method has a callback parameter for handling errors.

    store.clear(callback);

    Optional

    This optional method is used to delete all sessions from the store. The callback should be called as callback(error) once the store is cleared.

    You can use it like this:

    store.clear((error) => console.error(error));
    

    Maybe you will get more information with this.

    But according to your answer, you have to set it to get it working.