Skip to content
Tcl Source Code Artifact [57da73926c]
Login

Artifact [57da73926c]

Artifact 57da73926c11dfbdf205e394a976ad02690a66a451f8175ead7f0295e3a9e4d1:

Ticket change [57da73926c] - New ticket [a58737f57bf74097|a58737f57b] <i>The macOS implementation of Tcl_WaitForEvent is incorrect.</i>. by marc_culler on 2020-07-10 22:08:56.
D 2020-07-10T22:08:56.308
J assignee nobody
J closer nobody
J cmimetype text/plain
J comment The\splatform\sspecific\sfunction\sTcl_WaitForEvent\s(in\stclMacOSXNotify.c\sfor\r\nmacosx)\sis\scalled\sfrom\sTcl_DoOneEvent,\sthe\score\sof\sthe\sTcl\sevent\sloop.\s\sIt\r\ngets\scalled\sin\sTcl_DoOneEvent\safter\sfirst\schecking\sthat\sthere\sare\sno\r\nasynchronous\sevents\sand\sno\swindow\sevents\squeued.\sBefore\scalling\sTcl_WaitForEvent\r\nthe\ssetup\sproc\sfor\seach\savailable\sevent\ssource\sis\scalled.\sThen\sthe\scheck\sproc\r\nis\scalled\sfor\seach\sevent\ssource.\s\sThe\smain\sjob\sof\sthe\ssetup\sproc\sis\sto\sset\sthe\r\ntimeout\svalue\swhich\swill\sbe\spassed\sto\sTcl_WaitForEvent.\s\sOn\smacOS\sif\sthere\r\nare\sany\sevents\sin\sthe\sApple\swindow\sevent\squeue\sthen\sthe\stimeout\sis\sset\sto\s0,\r\nmeaning\sthat\sTcl_WaitForEvent\sshould\scheck\sfor\sfile\sevents\sand\sreturn\r\nimmediately\swith\sa\spositive\sreturn\svalue\sif\sany\sfile\sevents\shave\sbeen\r\nrecorded\sby\san\sauxiliary\snotification\sthread\swhich\sruns\sselect\sin\sthe\r\nbackground\sand\srecords\sany\sactive\sfiles\sdescriptors\sin\sa\sthread\sspecific\r\ndata\sstructure.\s\sIf\sthe\sApple\swindow\sevent\squeue\sis\sempty\sthen\sthe\stimeout\r\nwill\sbe\snon-zero\sand\sTcl_WaitForEvent\swill\swait\suntil\sthe\stimeout\sexpires.\r\n\r\nIn\sactual\sfact,\showever,\sthe\soriginal\sCocoa\simplementation\sof\sTcl_WaitForEvent\r\ndid\snot\sreturn\simmediately\swhen\sthe\stimeout\swas\sset\sto\s0.\s\sInstead,\sbefore\r\npolling\sfor\sfile\sevents,\sthe\sfollowing\sloop\swas\srun:\r\n\r\n\swhile\s(Tcl_ServiceAll()\s&&\stsdPtr->waitTime\s==\s0)\s{}\r\n\r\nThis\scode\spredates\sthe\srelease\sof\sCocoa\sand\swas\sprobably\sfollowing\sthe\r\ninstructions\sin\sthe\sTcl_ServiceAll\sman\spage\sregarding\show\sto\sembed\sTcl\sin\r\nan\sapplication\swhich\shas\sits\sown\sprivate\sevent\sloop.\sThere\sare\stwo\sissues\r\nhere.\s\sFirst,\sthe\sman\spage\sdoes\snot\srecommend\scalling\sTcl_ServiceAll\sin\sa\sloop.\r\nDoing\sthat\swould\sappear\sto\srun\sthe\srisk\sof\sa\shang,\sin\scases\swhere\sservicing\r\none\sevent\scauses\sa\snew\sevent\sto\sbe\squeued.\sIndeed,\sthere\swere\smany\sreports\r\nof\smacOS\shanging\sand\sKevin\sWalzer\sfound\sthat\scalling\sTcl_ServiceAll\sonce,\r\nnot\sin\sa\sloop,\sreduced\sthe\sincidence\sof\shangs.\s\sSo,\sin\scommit\s[c699f98468]\r\nin\s2015,\sKevin\sremoved\sthe\sloop\sand\sreplaced\sit\sby\sa\ssingle\scall\swith\r\nnoticeable\simprovement\sin\sthe\sbehavior.\r\n\r\nThe\ssecond\sissue\sis\smore\ssubtle.\s\sWhile\sit\swas\sprobably\strue\sin\sthe\sCarbon\r\nimplementation\sof\sTcl\sthat\sTcl\sactually\swas\sbeing\sembedded\sin\san\sapplication\r\nwith\sa\sprivate\sevent\sloop,\sI\sthink\sthat\sceased\sto\sbe\sthe\scase\swith\sthe\srelease\r\nof\sCocoa.\s\sIt\sis\strue\sthat\sthe\sNSApplication\sobject\shas\sa\sprivate\sevent\sloop\r\nwhich\scan\sbe\sstarted\sby\scalling\sthe\sfunction\srun().\s\sBut\sTcl\snever\scalls\sthat\r\nfunction.\sThe\srun()\smethod\shandles\sdispatching\sof\sApple\sNSEvents\sto\sAppKit\r\nobjects\ssuch\sas\sNSView\sand\sNSWindow.\s\sBut\sthat\swas\snot\shelpful\sfor\sTk\ssince\r\nwhat\swas\sneeded\swas\sto\stranslate\sNSEvents\sinto\sXEvents\sand\sadd\sthem\sto\sthe\r\nTcl\squeue\sso\sTk\scould\sprocess\sthem.\s\sInstead\sof\srunning\sApple's\sprivate\sevent\r\nloop,\sthe\sCocoa\simplementation\sof\sTk\semulates\sthe\sApple\srun()\sloop\swithin\sthe\r\ncheck\sproc\sfor\sthe\sAqua\sevent\ssource.\s\sThe\scheck\sproc\sremoves\sNSEvents\sfrom\r\nthe\sAqua\squeue\smanually\sand\stranslates\sthem\sinto\sXEvents\sbefore\scalling\r\nNSApplication\ssendEvent\sto\sallow\sthe\sNSEvents\sto\sbe\sprocessed\sby\sthe\swindow\r\nmanager.\s\sThe\sadaptation\sof\sTclWaitForEvent\sto\saccount\sfor\sthis\schange\swas\r\nnever\sdone.\r\n\r\nWhile\sKevin's\schange\sin\s2015\sreduced\sthe\snumber\sof\shangs\sobserved\sin\sTk\r\nconsiderably,\sthey\sstill\spersisted.\s\sMoreover,\sthe\sdesign\sof\sTcl_WaitForEvent\r\ncaused\sa\slot\sof\srandomness\sin\swhen\sthe\s[NSView\sdrawRect]\smethod\swas\sbeing\r\ncalled.\s\sAfter\sChristopher\sChavez\snoticed\sthat\sthe\sdrawRect\smethod\sonly\sgets\r\nrun\sduring\scalls\sto\sCFRunLoopinMode\sor\sNSNextEventMatchingMask\s(used\sin\sthe\r\nsetup\sproc\sand\sthe\scheck\sproc)\sI\swas\seventually\sled\sto\sstudy\swhat\swas\sactually\r\nhappening\sin\sTcl_WaitForEvent.\s\s(With\sthe\srelease\sof\sOSX\s10.14\s(Mojave)\sit\sbecame\r\ncritical\sto\scontrol\swhen\sdrawRect\sgets\scalled\sbecause\sApple\smade\schanges\sfor\r\nthat\srelease\swhich\scaused\sit\sto\sbe\sthe\scase\sthat\sgraphics\scalls\swhich\swere\snot\r\nmade\sfrom\swithin\sdrawRect\swere\signored;\sthose\scalls\swould\shave\sno\seffect\son\sthe\r\nappearance\sof\sany\sapplication\swindows,\scausing\sTk\sto\sdo\snothing\svisible\sother\r\nthan\sdisplay\sempty\sblack\swindows.)\s\r\n\r\nMy\sconclusion\swas\sthat\sTclServiceAll\sshould\snever\sbe\scalled\sin\sTclWaitForEvent.\r\nThis\sis\sconsistent\swith\sthe\sfact\sthat\sTcl\sis\sreally\snot\sbeing\sembedded\sin\san\r\napplication\swith\sits\sown\sprivate\sevent\sloop.\r\n\r\nI\shave\smade\sthis\schange\sto\stclMacOSXNotify.c\sin\sthe\sbranch\snamed\smacOS_hangs.\r\nI\shave\stested\sextensively\sboth\swith\sregression\stests\sand\sreal\sapplications.\r\n\r\nThere\sare\sno\snoticeable\sbad\seffects\sin\sTcl.\s\sAll\sregression\stests\spass.\s\sThe\r\nbranch\salso\smakes\ssome\sunrelated\schanges\shaving\sto\sdo\swith\sthe\sfact\sthat\sthe\r\nNSSpinLock\swas\sdeprecated\sby\sApple\swith\sthe\srelease\sof\sOSX\s10.15\s(Catalina)\sand\r\nreplaced\sby\sthe\sos_unfair_lock.\s\sThe\sadditional\schanges\sallow\sthe\suse\sof\r\nos_unfair_lock\sin\splace\sof\sNSSpinLock\son\ssystems\swhich\ssupport\sthis\smore\sefficient\r\nnew\slock\stype.\r\n\r\nThis\schange\sto\sTcl,\stogether\swith\schanges\sto\show\s[NSView\sdrawRect]\sis\shandled\r\nin\sTk,\sled\sto\ssignificant\simprovements\sin\sTk.\s\sSome\sregression\stest\sfailures\r\ndisappeared\sincluding\sthose\sreported\sin\s[06d8246baf]\sand\s[37a27ddb2]\sand\r\n[aedc9f6fa4]\sas\sdid\smany\sgraphical\sartifacts\swhich\swere\sreported\sfor\sexample\r\nin\stickets\s[2a61eca3a].\s\sThese\schanges\sare\sin\sthe\sTk\sbranch\sidle_curiosity.\r\n\r\nI\shave\sextensively\stested\sthe\scombination\sof\sidle_curiosity\sand\smacOS_hangs\r\nfor\sseveral\smonths\sas\shas\sNicolas\sBats\sand,\sI\sbelieve,\sothers.\s\sI\swould\slike\r\nto\ssee\sboth\sof\sthese\smerged\sinto\sthe\score_8_6_branches\sbefore\sthe\srelease\sof\r\n8.6.11.\s\sI\swould\sbe\svery\sgrateful\sfor\sreviews,\scomments\sand\stesting.
J foundin 10.6.10
J is_private 0
J login marc_culler
J priority 5\sMedium
J private_contact 4a267845b96d18ced148980dad8c23b791785018
J resolution None
J severity Important
J status Open
J submitter marc_culler
J subsystem 02.\sEvent\sLoops
J title The\smacOS\simplementation\sof\sTcl_WaitForEvent\sis\sincorrect.
J type Bug
K a58737f57bf740979e0ed091118508188b8c28f5
U marc_culler
Z a66ebbd9d546f8ba4afad5726afd6dff