Normal Topic Inconsistent function return (Read 1010 times)
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Inconsistent function return
Jun 19th, 2006 at 11:57pm
Print Post Print Post  
I have a created a function that is behaving inconsistently, seems like some type of race condition.
I am parsing script in a function and sending back a string.  Sometime the strng is being received, sometimes it receives a zero.  I have used WrileLn to try to analyze this.  The function is being called 9 times in a row, no delay between calls.  Seven lines come back correctly, but two always come back with a zero.  The problem is repeatable and is always zero for the same result, not the nth return.

Here is edited otuput showing good and bad lines using WriteLn:
Quote:
(Good)
1. vEnvValueString SESAME_REPORT_PATH=C:\Windows\Temp had vPos value of 19
3. vPos = not zero4. Sending vEnvValueString = C:\Windows\Temp
5. Received C:\Windows\Temp

(Bad)
1. vEnvValueString BAD-BAD-BAD  had vPos value of 0
2. vPos = zero
4. Sending vEnvValueString = Environment variable SESAME_ROOTDIRS_PATH not found
5. Received 0

(Good)
1. vEnvValueString BAD-BAD-BAD  had vPos value of 0
2. vPos = zero
4. Sending vEnvValueString = Environment variable SESAME_PRINT_COMMAND not found
5. Received Environment variable SESAME_PRINT_COMMAND not found

(Bad)
1. vEnvValueString BAD-BAD-BAD  had vPos value of 0
2. vPos = zero
4. Sending vEnvValueString = Environment variable SESAME_PRINT_PREVIEW_COMMAND not found
5. Received 0

Lines 1-4 are from function code.  Line 5 is from program calling the function.
If Line 1 =0, should go to Line2.  This works OK
If Line1 <>0 should go to Line3.  This works OK
Line 4 is the value of the function being sent back to the calling program (last line in function code)
Line 4 is the function value being received by the function. (immediately after running function in mass update code)
Note that in the first group, the line "4....." did not start on a new line, symptom of race condition?.

This is a Mass Update that is being done.
The function is in the Global Code section of the MUD program.  It is in the Global Code of Main program, but I found that the Mass Update could not call the function from the Main Global Code.  So I copied the code from the Main Program into the Mass Update Global Code section.    Hmmmm, perhaps that is the problem, cannot normally use Global Code for Mass Updates, perhaps I need to put the function before the Mass Update code..... I will try that next......
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Inconsistent function return
Reply #1 - Jun 20th, 2006 at 12:08am
Print Post Print Post  
Bob,

1. You can use the GLOBAL CODE area in a Mass Update the same way you can use that area in a Form.

2. Seeing your debug without being able to see the code that produced it makes it difficult to determine where the problem is. For example, what you ask is a symptom of a race condition may simply be you calling Write() instead of WriteLn(). Can we see the code?
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Inconsistent function return
Reply #2 - Jun 20th, 2006 at 12:29am
Print Post Print Post  
Also, looking at your debug, it looks like you are asking the system for the values of certain environment variables. It also looks like you are asking for them one at a time. Can you describe the way in which you are launching multiple competing processes that might be creating the suspected race condition? I can't see just from the debug.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Inconsistent function return
Reply #3 - Jun 20th, 2006 at 12:57am
Print Post Print Post  
You are correct about Write vs. WriteLn.  I just found that, and was back here to say fuhgeddaboutit. 
But that was a red herring still have the basic problem of  function call return not good. 

Here is the critical code for Function fnGetEnvVariable(vEnvVariable as String) as String :
Code
Select All
//Read the file line and delete the file
vFileHandle1 = FileOpen(vEnvFile)
FileSeek(vFileHandle1,0)
FileReadLn(vFileHandle1, vEnvValueString)
FileClose(vFileHandle1)
FileDelete(vEnvFile)

//Parse out the variable, from position of "=" to end of string
vLen = @Len(vEnvValueString)
vPos = @InStr(vEnvValueString,"=")

//Insertion of WriteLn lines to diagnose inconsistent output
WriteLn("1. vEnvValueString " + vEnvValueString + " had vPos value of "+ vPos)

If vPos = 0 THEN {

	WriteLn("2. vPos = zero")
	vEnvValue = "Environment variable " + vEnvVariable + " not found"
	} ELSE {

	WriteLn("3. vPos = not zero")
	vEnvValue = @MID(vEnvValueString, vPos + 1, vLen-vPos)
	}

WriteLn("4. Sending vEnvValueString = " + vEnvValue)
Return vEnvValue

End Function 

The file that is being read above is a single line from SET being redirected to a text file.  Example: "SESAME_REPORT_PATH=C:\Windows\Temp" without the quotes.
Just looking for an "=" sign to confirm existence of environment variable.  Picking up 9 variables.
This all looks good out of the function.  WriteLn 4 is always correct.  I have included the WriteLn to help diagnose (Write has been fixed).

And this is the calling section:
Code
Select All
/  Environment variables that need fnGetEnvVariable(vEnvVariable as String) as String included in MUD Global Code
var vComputer as String		//Environment variable for COMPUTERNAME
var vAllowFile as String	//Environment variable for SESAME_ALLOW_FILE
var vDenyFile as String		//Environment variable for SESAME_DENY_FILE
var vHelpPath as String		//Environment variable for SESAME_HELP_PATH
var vLicensePath as String	//Environment variable for SESAME_LICENSE_PATH
var vReportPath as String	//Environment variable for SESAME_REPORT_PATH
var vRootDirsPath		//Environment variable for SESAME_ROOTDIRS_PATH
var vPrintCommand as String	//Environment variable for SESAME_PRINT_COMMAND
var vPrintPreviewCommand	//Environment variable for SESAME_PRINT_PREVIEW_COMMAND

//Get Environment variables
vComputer = fnGetEnvVariable("COMPUTERNAME")
vAllowFile = fnGetEnvVariable("SESAME_ALLOW_FILE")
vDenyFile = fnGetEnvVariable("SESAME_DENY_FILE")
vHelpPath = fnGetEnvVariable("SESAME_HELP_PATH")
vLicensePath = fnGetEnvVariable("SESAME_LICENSE_PATH")

vReportPath = fnGetEnvVariable("SESAME_REPORT_PATH")
WriteLn("5. Received " + vReportPath)

vRootDirsPath = fnGetEnvVariable("SESAME_ROOTDIRS_PATH")
WriteLn("5. Received " + vRootDirsPath)

vPrintCommand = fnGetEnvVariable("SESAME_PRINT_COMMAND")
WriteLn("5. Received " + vPrintCommand)

vPrintPreviewCommand = fnGetEnvVariable("SESAME_PRINT_PREVIEW_COMMAND")
WriteLn("5. Received " + vPrintPreviewCommand)
 


Seven of the variables come back OK, but ROOTDIRS and PRINTPREVIEW ones come back as zeros.
I have tried running these in different sequence, no change.  I did move the function into the Element section with the calling code, but no change.  So I have moved the function back into the Global Code section.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Inconsistent function return
Reply #4 - Jun 20th, 2006 at 1:01am
Print Post Print Post  
I just saw the errior!

It helped to post here and read it in large type and different color.  The problem stood right out.

The two lines that are not good are not declared as STRING!   Embarrassed   Embarrassed   Embarrassed

Sorry about the distraction, but thanks for the support.  Sometimes it just needs another set of ears to listen.
Need to take a break, been working on this for too long
Roll Eyes   Roll Eyes

Well, I also was able to confirm that you can put functions into GLOBAL CODE when doing a Mass Update.

And thanks again for listening, problem solved because you asked me to show you the code that I have been looking at for hours!   Arrgh!   Embarrassed
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Inconsistent function return
Reply #5 - Jun 20th, 2006 at 1:31am
Print Post Print Post  
Bob_Hansen wrote on Jun 20th, 2006 at 1:01am:
The two lines that are not good are not declared as STRING!


Whenever you are expecting text and get a numeric zero instead, look for this. I get this one when I turn a subroutine into a function and forget to specify a return type.  Smiley
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged