Skip to content
Tcl Source Code Artifact [d5f544f400]
Login

Artifact [d5f544f400]

Artifact d5f544f400e85b9829a6876f4d6a48563a6f85881aa6e920d622b15c32fbd4ce:

Ticket change [d5f544f400] - Ticket [87271f7cd6ab29a0|87271f7cd6] <i>TclOO + coroutine + event loop = coredump</i> status still Open with 3 other changes by apnadkarni on 2024-05-06 17:14:14.
D 2024-05-06T17:14:14.466
J icomment This\sbug\sexists\sin\s9.0\sas\swell.\sBelow\sis\sthe\sdiagnosis.\sThis\sis\smy\sfirst\slook\sat\r\nthe\stcloo\simplementation\sso\scaveat\semptor!\sApologies\sfor\sthe\slength.\sIt\sis\sto\r\nkeep\sthings\sstraight\sin\smy\sown\shead\sas\sto\swhat\sis\sgoing\son.\r\n\r\nWhen\sa\soo\smethod\sis\screated,\sa\s`ProcedureMethod`\sstructure\sis\sallocated\sand\r\ninitialized\s(`pmPtr`\sin\sthe\sanalysis\sbelow).\sA\s`Proc`\sstructure\scontaining\sthe\sarg\r\ndefinitions,\sbyte\scode\setc.\shangs\soff\sthe\sprocPtr\sfield\sin\sthe\sstructure.\r\nCrucially,\sthe\s`procPtr->cmdPtr`\sis\s(by\sdesign)\sset\sto\sNULL\sand\sis\spopulated\r\nwhen\sthe\smethod\sis\sactually\scalled.\s**Note\sthis\sdatum\sis\spart\sof\sthe\sper-method\r\n"static"\sdata\s(not\son\sstack)\sand\sthat\sis\san\sobvious\sproblem\sas\srecursive\r\ncalls\swill\soverwrite\sthe\svalue.**\sMore\son\sthis\sbelow.\r\n\r\nWhen\sthe\smethod\sis\scalled\s(InvokeProcedureMethod),\sa\sPMFrameData\sstruct\sis\r\nallocated\son\sthe\sTcl\sstack.\sThere\sare\stwo\sfields\srelevant\sto\sthis\sbug.\sThe\sfirst\r\nis\sthe\s`cmd`\sfield\swhich\sis\sa\s`Command`\sstruct.\sOn\sa\smethod\scall,\sthis\sstruct\r\nis\sfilled\sout,\sas\sthe\scomment\sin\sthe\scode\r\nstates,\swith\smostly\sbogus\svalues,\sthe\svalid\sones\sbeing\sthe\sones\r\n(afaict)\sthat\sare\sneeded\sto\sdescribe\sthe\sframe.\s**The\s`procPtr->cmdPtr`\sfield\r\nabove\s(part\sof\sthe\sper-method\sdata)\sis\sthen\sset\sto\spoint\sto\sthis\sfield.**\r\n\r\nThe\sissue\swith\sthe\s`procPtr`\sfield\sbeing\spart\sof\sthe\smethod\sdata\sand\snot\son\sany\r\nstack\sresulted\sin\sthe\scrash\son\srecursive\scalls\sas\sreported\sin\sbug\s[3001438].\r\nFixing\sthis\sintroduced\sthe\ssecond\srelevant\sfield\sin\sPMFrameData\s-\s`oldCmdPtr`.\r\nOn\sa\smethod\scall,\sthe\scurrent\svalue\sstored\sin\sthe\smethod\sdata\r\n`pmPtr->procPtr->cmdPtr`\sfield\sis\sstored\sin\sthe\s`oldCmdPtr`\sfield\sand\r\n`pmPtr->procPtr->cmdPtr`\sis\supdated\sto\spoint\sto\sthe\s"dummy"\s`PMFrameData.cmd`\r\nfield\sbefore\sthe\sactual\smethod\sevaluation.\s**After\sthe\smethod\sevaluation,\r\nthe\sreverse\shappens\swith\s`pmPtr->procPtr->cmdPtr`\sbeing\srestored\sfrom\r\n`oldCmdPtr`\sto\sits\soriginal\svalue\sat\sthe\stime\sof\sthe\smethod\sinvocation.**\r\nEssentially\sa\slinked\sstack.\r\n\r\nThis\ssolved\sthe\s[3001438]\srecursive\scall\scrash\sresulting\sfrom\s**nested**\r\ncalls.\sHowever,\sin\sthe\scurrent\sbug\sreport,\sthe\scalls\sare\snot\snested,\sbut\r\n**interleaved**\svia\scoroutines\sresulting\sin\sinterference\sbetween\stwo\r\nconcurrently\sexecuting\smethod\scalls.\sThe\srestoration\sof\s`cmdPtr`\sduring\r\nthe\sreturn\sfrom\sthe\sfirst\scoroutine\scall\soverwrites\sthe\s`cmdPtr`\splaced\sin\sthe\r\nProcedureMethod\sstructure\sat\sthe\sstart\sof\sinvocation\sof\sthe\ssecond\scoroutine.\r\n(Remember\sthe\ssecond\scoroutine\sis\sstill\sin\sexecution\sstate)\r\nSince\sthe\soriginal\s`cmdPtr`\sat\smethod\sdefinition\stime\sis\sNULL,\sthe\sinvocation\r\nfrom\sthe\ssecond\scoroutine\sresults\sin\sa\sNULL\spointer\saccess.\r\n\r\nThe\ssequence\sis\sillustrated\safter\sthe\stest\scode\sbelow.\sThe\scode\sis\sslightly\ssimplified\sfrom\sthe\r\noriginal\swith\sunnecessary\sfragments\sremoved.\r\n\r\n```\r\n#\sat\sleast\s2\sinstances\sneeded\sfor\scoredump\r\nset\snworkers\s2\r\nset\snrounds\s1\r\n\r\nproc\smain\s{}\s{\r\n\s\s\s\sglobal\snworkers\r\n\s\s\s\sfor\s{set\sn\s1}\s{$n\s<=\s$nworkers}\s{incr\sn}\s{\r\n\s\s\s\s\s\s\s\sA\screate\sa$n\r\n\s\s\s\s\s\s\s\sputs\s"a$n\snamespace\s[info\sobject\snamespace\sa$n]"\r\n\s\s\s\s\s\s\s\sa$n\sschedule\r\n\s\s\s\s}\r\n\s\s\s\safter\s5000\s{puts\sSTOPPING;variable\sforever\sfalse}\r\n\s\s\s\svariable\sforever\strue\r\n\s\s\s\sputs\svwaiting\r\n\s\s\s\svwait\sforever\r\n\s\s\s\sreturn\s0\r\n}\r\n\r\noo::class\screate\sA\s{\r\n\s\s\s\smethod\sschedule\s{}\s{\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sschedule\sEnter"\r\n\s\s\s\s\s\s\s\sset\scoro\s::coro-[string\strim\s[self]\s:]\r\n\s\s\s\s\s\s\s\sif\s{[llength\s[info\scommands\s$coro]]\s==\s0}\s{\r\n\s\s\s\s\s\s\s\s\s\s\s\scoroutine\s$coro\smy\sWork\r\n\s\s\s\s\s\s\s\s}\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sschedule\sExit"\r\n\s\s\s\s}\r\n\s\s\s\smethod\sWork\s{}\s{\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sWork\sEnter"\r\n\s\s\s\s\s\s\s\safter\s0\s[info\scoroutine]\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sWork\syielding"\r\n\s\s\s\s\s\s\s\syield\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sWork\sback\sfrom\syield"\r\n\s\s\s\s\s\s\s\sglobal\snrounds\r\n\s\s\s\s\s\s\s\sputs\s"[self]\scalling\sdump"\r\n\s\s\s\s\s\s\s\sdump\r\n\s\s\s\s\s\s\s\s#\swith\s"after\s0\s[self]\sdump-frames"\sthere\sis\sno\scoredump\r\n\s\s\s\s\s\s\s\sputs\s"[self]\sWork\sExit"\r\n\s\s\s\s}\r\n}\r\n\r\nproc\sdump\s{}\s{\r\n\s\s\s\sputs\s"INFO\sFRAME=[info\sframe]"\r\n\s\s\s\sfor\s{set\si\s1}\s{$i\s<\s[info\sframe]}\s{incr\si}\s{\r\n\s\s\s\s\s\s\s\sputs\s[format\s"\\t%2d:\s%s"\s$i\s[info\sframe\s$i]]\r\n\s\s\s\s}\r\n}\r\n\r\nmain\r\n\r\n```\r\n\r\nThis\sresults\sin\sthe\sfollowing\ssequence\sresulting\sin\sthe\s`cmdPtr`\sbeing\sNULL\r\n(a1\sand\sa2\sare\sthe\stwo\sobjects\screated)\r\n\r\n1.\sa1\sis\screated.\sAt\sthis\stime\sthe\spmPtr->procPtr->cmdPtr\scorresponding\sto\r\na1.Work\sis\sinitialized\sto\sNULL.\r\n\r\n2.\sa1.Work\sis\scalled\s(from\sa1.schedule).\sThe\scurrent\svalue\sof\spmPtr->procPtr->cmdPtr\r\n(which\sis\sNULL\sfrom\sthe\smethod\sdefinition\stime)\sis\ssaved\sto\sfdPtr->oldCmdPtr\sand\r\na\snew\svalue\s&fdPtr->cmd\sis\sassigned\sto\sit\s(the\s"bogus"\sor\s"dummy"\sCommand\sstruct).\r\n\r\n3.\sa1.Work\sarranges\sfor\sitself\sto\sbe\scalled\sback\sthrough\sthe\sevent\sloop\sand\sthen\r\nyields\sback\sto\sa1.schedule\swhich\sreturns.\r\n\r\n4.\sa2\sis\screated.\r\n\r\n5.\sSteps\s2\sand\s3\sare\srepeated\sbut\sfor\sa2.\sNote\showever\sthat\spmPtr->procPtr->cmdPtr\r\nis\snot\sNULL\shere,\sit\sholds\sthe\svalue\sfrom\sthe\sa1.Work\scall!\sThis\sis\snot\r\nconsistent\sbut\swill\signore\sfor\sthe\smoment\sbecause\sit\sis\snot\sthe\scause\sof\sthe\scrash.\r\n\r\n6.\sCalls\sunwind\sto\sthe\smain\sthread\swhich\senters\sa\svwait\sloop.\sNow\sthe\sfun\sbegins.\r\n\r\n7.\sa1.Work\sreturns\sfrom\sthe\syield\s(via\sthe\scoroutine\scall\sfrom\sthe\sevent\sloop).\r\nIt\sdumps\sthe\strace\sinformation.\sThen\sduring\smethod\sexit,\s**it\srestores\swhat\sit\r\nsaved\sin\sits\sfdPtr->oldCmdPtr\sback\sinto\spmPtr->procPtr->cmdPtr**!\sNote\sthe\r\nlatter\sis\smethod\sdata\sand\sshared\swith\sthe\sinvocation\sof\sa2.Word.\sMoreover,\sthe\r\nvalue\sof\s`oldCmdPtr`\s(from\sstep\s2)\sis\sNULL.\sControl\sis\snow\sreturned\sback\sto\sthe\r\nevent\sloop.\r\n\r\n8.\sa2.Work\snow\sreturns\sfrom\sits\syield\s(again\svia\sfrom\sthe\sevent\sloop).\sIt\stoo\scalls\sdump.\r\n**But\snow,\sthe\spmPtr->procPtr->cmdPtr\svalue\sit\sset\sup\sin\sits\scall\sinitialization\r\nhas\sbeen\soverwritten\sby\sNULL\sas\spart\sof\sa1.Work\sexit\sin\sstep\s7.**\r\n\r\n9.\sCrash\sin\sTclInfoFrame\son\sattempt\sto\saccess\spmPtr->procPtr->cmdPtr.\r\n\r\nAbove\sis\seasily\scorroborated\swith\sa\sfew\sputs\sin\sthe\stest\scase.\r\n\r\nFixing\sthe\sspecific\scrash\sas\sdkf\smentioned\sin\san\searlier\spost,\scould\sbe\r\n\r\n1.\sJust\scheck\sfor\sthe\sNULL\spointer\sin\sTclInfoFrame\sand\signore\sthe\sframe\sor\sdo\ssome\sdefault.\r\nAside\sfrom\svery\smuch\sbeing\sa\sband\said\sand\snot\spalatable,\sI\sdo\snot\sknow\sthe\r\nrepercussions\son\susers\sof\sinfo\sframe.\r\n\r\n2.\sFix\sthe\score\sissue.\sDKF\sor\ssomeone\swith\sbetter\sknowledge\sof\sthe\soo\r\nimplementation\sthan\sme\s(everything\sI\sknow\sis\sup\sabove!)\swill\shave\sto\sdo\s(2).\sI'm\r\nlikely\sto\sintroduce\smore\sbugs\sthan\sI\sfix.\r\n\r\nAlthough\s`info\sframe`\sitself\smay\sor\smay\snot\sbe\simportant,\sI'm\smore\sworried\sabout\r\npossibly\smore\sserious\simplications\sof\sinterleaved\soo\smethod\scalls\sfrom\sdifferent\r\ncoroutines\sgetting\stangled\sup.
J login apnadkarni
J mimetype text/x-markdown
K 87271f7cd6ab29a076b36e31041a8c940c20d943
U apnadkarni
Z c60672f5103f5f024abb42658df3ebc3