chris212 has asked for the wisdom of the Perl Monks concerning the following question:
I have a script that uses threading. It has a shared variable ($ret) that is used to indicate if an error has occurred. All the threads check that variable so that work will stop in the event of a fatal error.
Unfortunately I cannot post the script. I'm not able to create a test script to replicate the issue since it is very intermittent and only seems to happen with long runs (over 4 hours). It doesn't use much memory, so it doesn't seem to be a memory leak.
Basically the script will start a new thread to handle writing output, and the main thread will start possibly millions of threads to process 500 records each as they are read using a semaphore to limit the number of concurrent threads. All threads check the value of the shared scalar. They can all modify the value, but didn't when these crashes occur.
>> /polaris_stg_root/dev/app/smartload/components/correctaddress_debug +/correctaddress_debug.pl:11: $Devel::Trace::TRACE = 0; SV = PVMG(0x10a6350) at 0xf68f68 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0xd5fb30 MG_VIRTUAL = 0x7f361a0c9320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0xee8f08 "" SV = PVMG(0x114ec30) at 0x114d558 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x1150a70 MG_VIRTUAL = 0x7f361a0c9320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0xee8f08 "" Attempt to free unreferenced scalar: SV 0xee8f08, Perl interpreter: 0x +ee6410. >> /polaris_stg_root/dev/app/smartload/components/correctaddress_debug +/correctaddress_debug.pl:884: exit($ret) if($ret == -1); # already fa +iled, don't compare counts or print stats panic: attempt to copy freed scalar ee8f08 to f68f68 at /polaris_stg_r +oot/dev/app/smartload/components/correctaddress_debug/correctaddress_ +debug.pl line 884. Attempt to free unreferenced scalar: SV 0xee8f68, Perl interpreter: 0x +ee6410. Attempt to free unreferenced scalar: SV 0xee8f08, Perl interpreter: 0x +ee6410.
One dump is the $ret variable from before the output thread returns. The other is from the main thread after all the input is read. I should have dumped it after the output thread is joined and will if I can make it crash again. The main thread does not have any references to $ret between dumping it and line 884.
UPDATE
I got a dump after the output thread is joined, and the refcount is still 5. There are no references to $ret until it crashes, so it seems the memory is freed even though the refcount is 5?
>> /polaris_stg_root/dev/data/QAS_TEST/correctaddress_debug.pl:11: $De +vel::Trace::TRACE = 0; Main thread before output thread finishes: SV = PVMG(0x19555c0) at 0x1822318 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x190b330 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" Output thread before returning: SV = PVMG(0x19f2730) at 0x1a3d280 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x1a3e2e0 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" Main thread after output thread finishes: SV = PVMG(0x19555c0) at 0x1822318 REFCNT = 5 FLAGS = (PADMY,GMG,SMG,IOK,pIOK) IV = 0 NV = 0 PV = 0 MAGIC = 0x190b330 MG_VIRTUAL = 0x7fe80f9de320 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 DUP LOCAL MG_PTR = 0x17a7b68 "" >> /polaris_stg_root/dev/data/QAS_TEST/correctaddress_debug.pl:884: ex +it($ret) if($ret == -1); # already failed, don't compare counts or pr +int stats panic: attempt to copy freed scalar 17a7b68 to 1822318 at /polaris_stg +_root/dev/data/QAS_TEST/correctaddress_debug.pl line 884. Attempt to free unreferenced scalar: SV 0x17a7b68, Perl interpreter: 0 +x1775870.
UPDATE 2
I changed the script to not use the global $ret variable (shared or not) from the input, output, or worker threads. This seems to keep my script from crashing. I may use marioroy's MCE approach in a future version.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: shared scalar freed early
by Corion (Patriarch) on Feb 22, 2017 at 17:29 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 19:57 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 22:20 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 18:00 UTC | |
by syphilis (Archbishop) on Feb 24, 2017 at 01:02 UTC | |
Re: shared scalar freed early
by ikegami (Patriarch) on Feb 22, 2017 at 20:18 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 01:45 UTC | |
by stevieb (Canon) on Feb 23, 2017 at 02:45 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 19:16 UTC | |
by BrowserUk (Patriarch) on Feb 23, 2017 at 20:17 UTC | |
by ikegami (Patriarch) on Feb 23, 2017 at 20:08 UTC | |
Re: shared scalar freed early
by BrowserUk (Patriarch) on Feb 22, 2017 at 17:26 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 18:13 UTC | |
by BrowserUk (Patriarch) on Feb 22, 2017 at 18:28 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 18:48 UTC | |
Re: shared scalar freed early
by Laurent_R (Canon) on Feb 22, 2017 at 18:22 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 18:52 UTC | |
by ikegami (Patriarch) on Feb 22, 2017 at 20:16 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 03:13 UTC | |
by marioroy (Prior) on Feb 25, 2017 at 11:03 UTC | |
| |
by marioroy (Prior) on Feb 26, 2017 at 01:52 UTC | |
| |
by ikegami (Patriarch) on Feb 23, 2017 at 20:33 UTC | |
| |
by marioroy (Prior) on Mar 08, 2017 at 06:00 UTC | |
| |
by marioroy (Prior) on Mar 03, 2017 at 23:52 UTC | |
by chris212 (Scribe) on Feb 22, 2017 at 23:02 UTC | |
by tye (Sage) on Feb 22, 2017 at 20:17 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 03:16 UTC | |
by tye (Sage) on Feb 24, 2017 at 04:53 UTC | |
| |
by Laurent_R (Canon) on Feb 22, 2017 at 21:56 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 01:49 UTC | |
by chris212 (Scribe) on Feb 23, 2017 at 03:19 UTC |