1282 Simultaneous script execution corrupts variables [6404]

This forum is for reporting bugs in MediaMonkey for Windows 4. Note that version 4 is no longer actively maintained as it has been replaced by version 5.

Moderator: Gurus

chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

1282 Simultaneous script execution corrupts variables [6404]

Post by chrisjj »

If I launch a script and then before completion launch it again, the variable writes within the second instance overwrite the first instance's variables.

Tracked at http://www.ventismedia.com/mantis/view.php?id=6404 .
Chris
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

At http://www.ventismedia.com/mantis/view.php?id=6404 Jiri wrote:
It's actually designed this way to prevent problems with several instances of scripts running - there's always only one instance of a script.
and I replied
> It's actually designed this way to prevent problems
> with several instances of scripts running

It doesn't prevent problems AFAICS. It causes them.

> there's always only one instance of a script.

Not true. I can launch two instances and both show on the status bar.
Am I the only one finding I can launch two instances of a script and both show on the status bar?
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

I know that I can, and I design my scripts to account for this.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

> I know that I can

Jiri, if you are reading this, please note: I think your statement is in error. And so I hope you will remove your RESOLVED status from this serious issue.

> and I design my scripts to account for this.

I have a scripting using many Dim variables that get corrupted by subsequent simultaneous instance. What's the best way allow two to run without this corruption? Failing which, to prevent two from running? Thanks.
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance as you have discovered because the global variables are the same references, therefore Jiri is correct in what he has said.

I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects". Sometimes I define a lot of global variables, for instance one for each of the settings I've stored in "MediaMonkey.ini", but I read them in from the ini file (or update them from the dictionary object) at the beginning of each Sub to make sure they are correctly populated.

The first thing a number of my scripts (such as "Genre Finder") do is check to see if SDB.Objects is already populated with a relevant dictionary and exits if it does.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

> By "I know that I can" I was referring the fact that a script could be run twice. They are run within the same instance

Ah. You actually said that in response to my

"Am I the only one finding I can launch two instances of a script ..."

> I only use global variables for constants and I store all global information in a dictionary object in "SDB.Objects".

Wow. That would really complicate and obfuscate my code. Likewise moving globals into locals, passing by reference.
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

I guess it depends on the interpretation of "instance", I wasn't thinking technically when I first responded. I always use SDB.Objects, seems to work well for me.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

> I always use SDB.Objects, seems to work well for me.

Thanks for the suggestion.

How would code using Dictionary objects this for example:

Code: Select all

Dim global1, global2, global3
...
global2 = "string2"
global3=  "string3"
...
global1 = global2 & global3
?
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

Code: Select all

Dim dict : Set dict = CreateObject("Scripting.Dictionary")
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Set SDB.Objects("MyDictionary") = dict
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

Thanks, but surely still 'dict' may get corrupted by this problem. And that corruption will then get transferred to SDB.Objects.
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

Sorry, maybe I should have given you a little more...

Code: Select all

Dim dict : Set dict = SDB.Objects("MyDictionary")
If Not (dict Is Nothing) Then
  Exit Sub 
End If
Set dict = CreateObject("Scripting.Dictionary")
Set SDB.Objects("MyDictionary") = dict
dict.Item("global2") = "string2"
dict.Item("global3") = "string3"
dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

Still corruption can occur e.g.

Code: Select all

Execution 1: dict.Item("global3") = "string3"
Execution 2: Set dict = CreateObject("Scripting.Dictionary")
Execution 1: dict.Item("global1") = dict.Item("global2")&dict.Item("global3")
Execution 1 will now find dict empty instead of containing the three strings.
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

Theoretically, but unless your computer is one of those full room sized ones from the the 80s you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
chrisjj
Posts: 5007
Joined: Wed Feb 14, 2007 5:14 pm
Location: UK

Re: 1282 Simultaneous script execution corrupts variables

Post by chrisjj »

trixmoto wrote:you don't have a hope in hell's chance of running the script a second time quick enough if that code is at the beginning.
And if it is at another point?? Or points?? e.g. being in a SUB called multiple times?? Or between the set and get of the variable are many lines of other code e.g. doing slow things like file I/O??

I would like to hear from Jiri if he believes it is at all possible to use global variables without this risk of corruption.
Chris
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Re: 1282 Simultaneous script execution corrupts variables

Post by trixmoto »

If you put it right at the beginning of each access point to your script then the script can only be running once within it's instance (as each subsequent to run will immediately exit out) and therefore there will be no corruption. Then at every exit point from your script you need to make sure that you set the SDB.Object back to "Nothing" so that the script then be run again.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Locked