Modules in WebApps

If I have a module ( e.g. a sorting method) in a WebApp, it’s code and variables are shared amongst all user sessions. This isn’t a problem normally (without threads), since two user sessions cannot call the same module at the same time. The second user will have to wait until the first user gives back control.

But what happens when the two user sessions are each in threads and they call the same module. For example, the first user’s thread ReDim’s the array data, provides the new array data then begins its sort. If a second user’s thread calls the same module before the first user is finished, then the second user can potentially ReDim the first user’s array, giving faulty results.

Is there a way to avoid this?

[quote=222082:@David Cox]If I have a module ( e.g. a sorting method) in a WebApp, it’s code and variables are shared amongst all user sessions. This isn’t a problem normally (without threads), since two user sessions cannot call the same module at the same time. The second user will have to wait until the first user gives back control.

But what happens when the two user sessions are each in threads and they call the same module. For example, the first user’s thread ReDim’s the array data, provides the new array data then begins its sort. If a second user’s thread calls the same module before the first user is finished, then the second user can potentially ReDim the first user’s array, giving faulty results.

Is there a way to avoid this?[/quote]

I would avoid using global variables like the pest in a Xojo Web app. But if you absolutely need to have common data, and do something like redimming an array, repopulate and sort, use some flag to avoid conflicts. For instance, add a string parameter “caller” like the session identifier and store it in a property until the array is released. Then make that property “”, so the next caller knows it can go ahead and use the array.

You will have to decide what happens, though, if the array is not available. For instance, should the caller give up or place the modifications in a thread so it takes effect when the array is released.

@Michel Bujardet Your workaround is feasible, but a huge effort to adjust every method within every method in every module I have (many dozens). I always pass all required parameters to the module’s method, so there is no need for global variables, but these local method variables can be compromised it seems.

Alternatively, is there a way to create your own incidence of a module, or attach it to a user’s Session, thus making each module call independent for each user session?

Unless I am severely mistaken, a module is by essence a global resource. Whatever the way you access the properties within (Global, public or protected). Call it common to all parts of the program if you will, there is always only one property for all areas of the app. If several sessions need to access one single property, then you must implement some collision management.

You could probably use a computed property instead of a simple array. That would enable you to implement a collision management within the setter.

What you are asking is like “can I have a module per session”. Not possible. But you can use session properties.

wouldn’t this be a good place to use semaphores?

They are explicitly designed to avoid such race conditions as two threads accessing the same resource when (at least) one of them should wait.