Skip to content

Commit

Permalink
BL-914 #resolve
Browse files Browse the repository at this point in the history
Null session scope when attempting to update the last visit
  • Loading branch information
lmajano committed Jan 14, 2025
1 parent ee5e31d commit cc022bc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/main/java/ortus/boxlang/runtime/application/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ private void startupSessionStorage( ApplicationBoxContext appContext ) {
.get( key )
.ifPresent( session -> ( ( Session ) session ).shutdown( this.startingListener ) );

logger.debug( "Session storage cache [{}] shutdown and removed session [{}]", targetCache.getName(), key );

return false;
}, BoxEvent.BEFORE_CACHE_ELEMENT_REMOVED.key() );
logger.debug( "Session storage cache [{}] created for the application [{}]", sessionCacheName, this.name );
Expand All @@ -378,6 +380,7 @@ private void startupSessionStorage( ApplicationBoxContext appContext ) {
public Session getOrCreateSession( Key ID, RequestBoxContext context ) {
Duration timeoutDuration = null;
Object sessionTimeout = context.getConfigItems( Key.applicationSettings, Key.sessionTimeout );
String cacheKey = Session.buildCacheKey( ID, this.name );

// Duration is the default, but if not, we will use the number as seconds
// Which is what the cache providers expect
Expand All @@ -390,12 +393,23 @@ public Session getOrCreateSession( Key ID, RequestBoxContext context ) {
// logger.debug( "**** getOrCreateSession {} Timeout {} ", ID, timeoutDuration );

// Get or create the session
return ( Session ) this.sessionsCache.getOrSet(
Session.buildCacheKey( ID, this.name ),
Session targetSession = ( Session ) this.sessionsCache.getOrSet(
cacheKey,
() -> new Session( ID, this ),
timeoutDuration,
timeoutDuration
);

// Is the session still valid?
if ( targetSession.isShutdown() ) {
// If not, remove it
this.sessionsCache.clear( cacheKey );
// And create a new one
targetSession = new Session( ID, this );
this.sessionsCache.set( cacheKey, targetSession, timeoutDuration, timeoutDuration );
}

return targetSession;
}

/**
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/ortus/boxlang/runtime/application/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class Session implements Serializable {
*/
private boolean isNew = true;

/**
* Flag for when session has been shutdown
*/
private boolean isShutdown = false;

/**
* The application name linked to
*/
Expand Down Expand Up @@ -244,21 +249,30 @@ public void shutdown( BaseApplicationListener listener ) {
if ( this.sessionScope != null ) {
this.sessionScope.clear();
}
this.sessionScope = null;
this.sessionScope = null;
this.isNew = true;
this.isShutdown = true;
}

}

/**
* Tests if the session is still active or shutdown
*/
public boolean isShutdown() {
return this.isShutdown;
}

/**
* Convert to string
*/
@Override
public String toString() {
return "Session{" +
"ID=" + ID +
", sessionScope=" + sessionScope +
", isNew=" + isNew +
", applicationName=" + applicationName +
"ID=" + this.ID +
", sessionScope=" + this.sessionScope.toString() +
", isNew=" + this.isNew +
", applicationName=" + this.applicationName +
'}';
}

Expand All @@ -269,7 +283,7 @@ public IStruct asStruct() {
return Struct.of(
Key.id, this.ID,
Key.scope, this.sessionScope,
"isNew", isNew,
Key.isNew, this.isNew,
Key.applicationName, this.applicationName
);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ortus/boxlang/runtime/scopes/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key interfaces = Key.of( "interfaces" );
public static final Key interrupted = Key.of( "interrupted" );
public static final Key interval = Key.of( "interval" );
public static final Key isNew = Key.of( "isNew" );
public static final Key invoke = Key.of( "invoke" );
public static final Key invokeArgs = Key.of( "invokeArgs" );
public static final Key invokeImplicitAccessor = Key.of( "invokeImplicitAccessor" );
Expand Down

0 comments on commit cc022bc

Please sign in to comment.