45693 - Web App SessionNotAvailableException After Dimming New WebSessionContext

Submitted a bug report: <https://xojo.com/issue/45693> - Web App SessionNotAvailableException After Dimming New WebSessionContext

Example Project: https://github.com/campsoftware/Xojo-Xanadu

Steps: - Download the project from: https://github.com/campsoftware/Xojo-Xanadu

  • Run the Project
  • Click Contacts to jump to the Contacts Module
  • Click random Contacts and eventually a SessionNotAvailableException occurs. I’ve seen it occur after clicking three contacts, but usually it takes more clicks.

The error occurs in the RecordContainerNewEmbed method of XanaduPortalContainer. It’s being called downstream from a Timer. The first line of the method, we call “Dim theSession As New WebSessionContext( Xan.UserSession )” to get access to the session.

Expected Result:
To not crash with a SessionNotAvailableException after calling “Dim theSession As New WebSessionContext( Xan.UserSession )”

Actual Result:
A crash ending in a SessionNotAvailableException

Workarounds:
Don’t use a Timer to call method that need access to the Session.

Video and Screenshot:

This is smack the same as what is currently discussed here : https://forum.xojo.com/36140-webthread-xojo-core-timer-calllater-and-websessio

Thanks Michel. I didn’t notice that post.

WebSessionContexts are tricky. They point to the right place right when you instantiate them and will continue to do so until they don’t. But looking at your code, I have a sneaking suspicion that you’re doing this to yourself.

Session contexts can change if:

  • A thread context change occurs. That is, the app yields the current thread to allow another thread time to do some processing.
  • Another WebThreadContext gets created. In your case, this could happen if you had two or more users selecting contacts or even a single user clicking too quickly.

Save yourself a headache and refactor this code to run in a WebThread as they stay connected to the thread from which they were instantiated and don’t need a WebSessionContext to do what you’re trying to do.

Maybe I am approaching this wrong. I’m cool with any way to implement

  • hide or disable controls and show a spinner or a progress bar
  • run my code that updates WebListBoxes, WebTextFields, and other WebControls
  • unhide or undisable controls and hide the spinner or progress bar

I used timers since you can update UI stuff in Timers and thought you could not update UI stuff like controls in Threads or WebThreads?

OH! Just read on https://forum.xojo.com/36140-webthread-xojo-core-timer-calllater-and-websessio/6#p295704 that you CAN do UI stuff in Web Apps with WebThreads!

The limitation of UI access with threads is specific to Desktop.

However, Web really waits to update the UI until the event is over. So for instance manipulation a label content from inside a loop as is possible in desktop, would simply not work in Web. So use of a timers remains handy.

@Paul Lefebvre : Would it make sense to add something to the WebThread docs that UI stuff can be done in a WebThread unlike the Desktop Thread?

I wouldn’t do that. People would then ask for it to be acceptable on Desktop.

Best practice would be to avoid UI stuff in a thread of any kind, it will help you keep your structure similar between web and desktop without a whole bunch of refactoring.

[quote=295852:@Tim Parnell]I wouldn’t do that. People would then ask for it to be acceptable on Desktop.

Best practice would be to avoid UI stuff in a thread of any kind, it will help you keep your structure similar between web and desktop without a whole bunch of refactoring.[/quote]

I see what you mean, but It states in the Thread docs very clearly that you CANNOT do any UI stuff and goes as far to show how to do it with a Timer. Right now the assumption in reading the docs that the WebThread is similar to Thread. If Thread and WebThread are different, it should be stated, in my opinion.

WebThreads aren’t magical - they DONT actually directly manipulate the UI
They change the DATA that will eventually be sent to the browser that then draws the UI
This is why they APPEAR to be able to change the UI in a thread

Well, Tim, avoiding UI update in Web from threads seems a bit extreme to me. There are reasons why it is possible in Web that do not exist in Desktop, and it would be a shame not to take advantage of Web specificities.

It would not hurt anyway. File a feature request. My experience is that Paul is very quick implementing good suggestions.

To avoid people to demand UI access in desktop thread, though, it would be profitable to have a phrase to that effect in the WebThread page, to stress the differences between platforms.

I updated my code from using WebTimers to WebThreads. But now I’m getting Javascript Null Is Not an Object when I embed containers.

I started a new thread since it’s a different issue:
https://forum.xojo.com/36201-webthread-webcontainer-embedwithin-javascript-null-is-not-an-ob

Why are you using the “New” operator with a webSession? - i wasn’t aware you could create a WebSession yourself

should it just be something like:

dim theSession as WebSessionContext = WebSessionContext(Xan.usersession)

[quote=296086:@Brock Nash]Why are you using the “New” operator with a webSession? - i wasn’t aware you could create a WebSession yourself

should it just be something like:

dim theSession as WebSessionContext = WebSessionContext(Xan.usersession)

No, Hal’s got it right. He’s creating a new WebSessionContext.