WebThread / Xojo.Core.Timer.CallLater and WebSession

Yup :slight_smile:

WebThreads can manipulate the UI in Web Projects.

Well that makes things easier, I couldn’t find much info on them so assumed the same as Desktop.

Think only place I might still need to use Xojo.Core.Timer is where I have a WebThread do some bits then when it’s complete I need to close the WebPage (the WebPage the WebThread is on) and open a different WebPage.

I don’t think I can do that directly from the WebThread as it would still be running.

I will try using the Session.Identifier in the Method used by Xojo.Core.Timer, something like this.

[code] Dim Page As WebPage

Dim context As WebSessionContext

’ Loop on Sessions
For i As Integer = 0 To App.SessionCount - 1

' Look for Session Identifier
If App.SessionAtIndex(i).Identifier = mySession Then
  
  ' Create Context
  context = new WebSessionContext(App.SessionAtIndex(i))
  
  ' Close Current Page
  Self.Close
  
  ' Define Select Page
  Page = New frmSelect
  
  ' Show Select Page
  Page.Show
  
  ' Exit For
  Exit For
  
End if

Next[/code]

I suspect that would work just fine. Just stop the run loop.

In the WebPage close event I was planning on doing

 If WebThreadX.Running = WebThread.Running Then WebThreadX.Kill

I’ll not have a WebSessionContext issue like you describe here? https://forum.xojo.com/36161-45693-web-app-sessionnotavailableexception-after-dimming-new-we

You don’t have to kill the thread. Just let the Run event of the thread actually finish when it’s done processing.

If it’s using a while loop, just set one of the conditions to the value of a property:

[code]while not Stop

…do your code here

Wend[/code]

When you’re ready to stop, set the Stop property to True.

I’ve now got rid of the the Xojo.Core.Timer.CallLater other than in the one place I mentioned and I still seem to be having issues.

The place I’ve been able to reproduce the issue so far isn’t the place using Xojo.Core.Timer.CallLater

[quote]Exception Message: Control cannot be created because no session context is available. Create a WebSessionContext object.
Exception Error Number: 0
An exception of class SessionNotAvailableException was not handled. The application must shut down.[/quote]

I’ve also seen at least two or three segmentation fault (core dumped)

Me and another user was testing the app at the same time and it crashed, so we tried again after a while it crashed again.

I’m able to reproduce this by having me and the other user clicking on the same button over and over, after a while we get the crash.

It also appears a single user doing this is also able to cause the same issue.

The button being clicked does the following

[code]Dim Page As WebPage

' Close Page
Session.CurrentPage.Close
Session.CurrentPage = Nil

' Show Please Wait
Session.PleaseWait.Show

' Create New Main Page
Page = New frmMain

' Show Main Page
Page.Show[/code]

In the shown event of the WebPage (frmMain) I start a WebTread which gets some data from a database and does some bits with it.

[code]Sub Shown()
’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + "WebPage Main by User (Shown Event) " + Session.UserMember.ToText

’ Load Record Types
ThreadLoadTypes.Run
End Sub[/code]

Once it’s finished working out what it needs to it then defines a container and embeds it, in that container is a WebListBox in it’s Shown Event I populate it with using RecTypes, RecTypes is populated by the WebThread.

The crash happens on ContainerMainT.EmbedWithin(Self, 0, 175, ContainerMainT.Width, ContainerMainT.Height)

[code] ’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + “Load Record Types by User” + " " + Session.UserMember.ToText + " " + “for Organisation” + " " + Session.UserOrganisation.Name + " - " + “Define Container”

' Define New Container
ContainerMainT = New ContainerControlMain

' Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + "Load Record Types by User" + " " + Session.UserMember.ToText + " " + "for Organisation" + " " + Session.UserOrganisation.Name + " - " + "Define Container Complete"

' Pass Types to Container ByRef
ContainerMainT.RecTypes = RecTypes

' Set Error Flag for Container
ContainerMainT.ErrorFlag = False

' Set Error Message for Container
ContainerMainT.ErrorMessage = ""

’ Set Lock Vertical/Horizontal
ContainerMainT.LockVertical = False
ContainerMainT.LockHorizontal = True

’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + “Load Record Types by User” + " " + Session.UserMember.ToText + " " + “for Organisation” + " " + Session.UserOrganisation.Name + " - " + “Embed Container”

Try

' Embed Container
ContainerMainT.EmbedWithin(Self, 0, 175, ContainerMainT.Width, ContainerMainT.Height)

Catch e as SessionNotAvailableException

If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + "Embed Container" + " " + "SessionNotAvailableException"

End Try

’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + “Load Record Types by User” + " " + Session.UserMember.ToText + " " + “for Organisation” + " " + Session.UserOrganisation.Name + " - " + “Embed Container Complete”[/code]

Output from all the Debug Messages

[code]2016-10-31 19:43:07 | WebPage Main by User (Shown Event) 267581
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Record Types | 4
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Load Complete
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Define Container
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Define Container Complete
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Embed Container
2016-10-31 19:43:07 | Load Record Types by User 267581 for Organisation X1 - Embed Container Complete
2016-10-31 19:43:08 | Container Main by User (Shown Event) 267581

2016-10-31 19:43:10 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:10 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:10 | WebPage Main by User (Shown Event) 267581
2016-10-31 19:43:10 | Load Record Types by User 267581 for Organisation X1
2016-10-31 19:43:10 | Load Record Types by User 267581 for Organisation X1 - Record Types | 4
2016-10-31 19:43:10 | Load Record Types by User 267581 for Organisation X1 - Load Complete
2016-10-31 19:43:11 | Load Record Types by User 267581 for Organisation X1 - Define Container
2016-10-31 19:43:11 | Load Record Types by User 267581 for Organisation X1 - Define Container Complete
2016-10-31 19:43:11 | Load Record Types by User 267581 for Organisation X1 - Embed Container
2016-10-31 19:43:11 | Load Record Types by User 267581 for Organisation X1 - Embed Container Complete
2016-10-31 19:43:11 | Container Main by User (Shown Event) 267581

2016-10-31 19:43:12 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:12 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:12 | WebPage Main by User (Shown Event) 267581
2016-10-31 19:43:12 | Load Record Types by User 267581 for Organisation X1
2016-10-31 19:43:12 | Load Record Types by User 267581 for Organisation X1 - Record Types | 4
2016-10-31 19:43:12 | Load Record Types by User 267581 for Organisation X1 - Load Complete
2016-10-31 19:43:12 | Load Record Types by User 267581 for Organisation X1 - Define Container
2016-10-31 19:43:13 | Load Record Types by User 267581 for Organisation X1 - Define Container Complete
2016-10-31 19:43:13 | Load Record Types by User 267581 for Organisation X1 - Embed Container
2016-10-31 19:43:13 | Load Record Types by User 267581 for Organisation X1 - Embed Container Complete
2016-10-31 19:43:13 | Container Main by User (Shown Event) 267581

2016-10-31 19:43:14 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:14 | WebPage Main by User (Close Event) 267581
2016-10-31 19:43:14 | WebPage Main by User (Shown Event) 267581
2016-10-31 19:43:14 | Load Record Types by User 267581 for Organisation X1
2016-10-31 19:43:14 | Load Record Types by User 267581 for Organisation X1 - Record Types | 4
2016-10-31 19:43:14 | Load Record Types by User 267581 for Organisation X1 - Load Complete
2016-10-31 19:43:15 | Load Record Types by User 267581 for Organisation X1 - Define Container
2016-10-31 19:43:15 | Load Record Types by User 267581 for Organisation X1 - Define Container Complete
2016-10-31 19:43:15 | Load Record Types by User 267581 for Organisation X1 - Embed Container

Exception Message: Control cannot be created because no session context is available. Create a WebSessionContext object.
Exception Error Number: 0
An exception of class SessionNotAvailableException was not handled. The application must shut down.

2016-10-31 19:43:15 | Load Record Types by User 267581 for Organisation X1 - Embed Container Complete[/code]

You’re really not doing yourself any favors with this technique. One severe issue is that you’re loading all of the data into ram and then populating the listbox which at least doubles the ram, and then until the data is completely sent, there’s actually a third copy in memory. I suspect that your segfault is really an out of memory issue.

A better technique would be to create and embed the container and then push data to the listbox 10 or 20 rows at a time. You’ll get better performance that way and the user will get data in front of their eyes almost immediately.

[quote=296154:@Greg O’Lone]You’re really not doing yourself any favors with this technique. One severe issue is that you’re loading all of the data into ram and then populating the listbox which at least doubles the ram, and then until the data is completely sent, there’s actually a third copy in memory. I suspect that your segfault is really an out of memory issue.

A better technique would be to create and embed the container and then push data to the listbox 10 or 20 rows at a time. You’ll get better performance that way and the user will get data in front of their eyes almost immediately.[/quote]

I take your point and will look at something like that but at present i’m only dealing with a test data and in the example above it was only 4 records and the server has plenty of free memory.

My main issue is the ongoing SessionNotAvailableException, even Try/Catch doesn’t help it still causes a crash.

Now I’m not using any timers and everything is being done within the WebThread what could cause these issues?

All the WebThread does is call a method that gets data from a database and processes it, then below the method call in the WebThread is the bits that embed the container.

[code]Try

' Embed Container
ContainerMainT.EmbedWithin(Self, 0, 175, ContainerMainT.Width, ContainerMainT.Height)

Catch e as SessionNotAvailableException

If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + "Embed Container" + " " + "SessionNotAvailableException"

End Try[/code]

Where is the WebThread created?

Was dragged onto the WebPage from the Library, the run event is started in the WebPage Shown Event.

I’m also seeing SessionNotAvailableException in my use of WebTimer and once with WebThread.

That’s very confusing to me. We’ll have to figure out how to reproduce that on my machine so I can trap the exception and see where it’s actually happening.

The WebTimer issue reproducible in my project: https://github.com/campsoftware/Xojo-Xanadu
When you run it, login as hal / hal, click contacts, then click one person at a time and it should occur with 15 clicks or less.

Since then, I recoded to use WebThreads which has a different issue which is also reproducible after 15 clicks or less. https://forum.xojo.com/36201-webthread-webcontainer-embedwithin-javascript-null-is-not-an-ob

I think both have to do with embedding containers.

I’ve check to make sure the super is WebThread, when dragging from the Library the super by default is Thread.

Doesn’t seem to be a new issue it’s happening on 2015 R3.1 and 2016 R3.

I’ve only seen that null issue once but that was after the application had given me the session not available exception.

Odd thing is before and after I embed the container I do a System.DebugLog which includes details from the Session and both work fine.

[code] ’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + “Load Record Types by User” + " " + Session.UserMember.ToText + " " + “for Organisation” + " " + Session.UserOrganisation.Name + " - " + “Embed Container”

Try

' Embed Container
ContainerMainT.EmbedWithin(Self, 0, 175, ContainerMainT.Width, ContainerMainT.Height)

Catch e as SessionNotAvailableException

If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + "Embed Container" + " " + "SessionNotAvailableException"

End Try

’ Debug Message
If App.DebugCheck Then System.DebugLog Xojo.Core.Date.Now.ToText + " | " + “Load Record Types by User” + " " + Session.UserMember.ToText + " " + “for Organisation” + " " + Session.UserOrganisation.Name + " - " + “Embed Container Complete”[/code]

I seem to recall in my testing that if I would dim var and set it to the session, it would work before and and after the embed like you saw.

That’s a huge problem and I’m going to get that fixed right now.

I see no reason why that would work.

Anyone that’s having this issue, it would be very helpful to know how far back this issue happened. I’ve reviewed web framework changes for the last six months and I don’t see anything that should cause this.

Are you able to reproduce the issues in my sample projects?