Skip to content
Tcl Source Code History Of Ticket 79474c58800cdf94
Login

History Of Ticket 79474c58800cdf94

Artifacts Associated With Ticket 79474c58800cdf94

  1. Ticket change [336d9452b4] (rid 173763) by mjanssen on 2019-07-31 13:22:05:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      The following script causes a segfault in DoRead:
      <verbatim>
      proc nexec {cmd in out stderr} {
          lassign [chan pipe] in1 out1
          lassign [chan pipe] in2 out2
          lassign [chan pipe] in3 out3
          puts [exec {*}$cmd >@ $out2 2>@ $out3 <@ $in1 &] 
          fileevent $in2 readable [list chan copy $in2 $out]
          fileevent $in3 readable [list chan copy $in3 $stderr]
          fileevent $out1 writable [list chan copy $in $out1]
      }
      
      proc test {args} {
        puts $args 
        set rest [lassign $args mode chan]
        switch -exact $mode {
          read {puts $chan "Test" ; close $chan}
          initialize {return "initialize watch finalize read write"}
          finalize {}
        }
      }
      
      set c [chan create "read write" test]
      
      nexec {sh -c "echo test"} $c $c $c
      
      vwait forever
      
      </verbatim>
      
      Gives with a debug build:
      
      <verbatim>
      % source exec-test.tcl
      initialize rc0 {read write}
      36514
      read rc0 4096
      write rc0 {Test
      }
      finalize rc0
      Assertion failed: (bufPtr != NULL), function DoRead, file /Users/mpcjanssen/Src/tcltk/tcl/generic/tclIO.c, line 9815.
      </verbatim>
      
    5. foundin initialized to: "8.6.9"
    6. is_private initialized to: "0"
    7. login: "mjanssen"
    8. priority initialized to: "5 Medium"
    9. private_contact initialized to: "9e3254e6e3c2643cc148bab2d55f46bb4c0446f4"
    10. resolution initialized to: "None"
    11. severity initialized to: "Minor"
    12. status initialized to: "Open"
    13. submitter initialized to: "mjanssen"
    14. subsystem initialized to: "25. Channel System"
    15. title initialized to: "refchan segfault"
    16. type initialized to: "Bug"
  2. Ticket change [1622194e55] (rid 173769) by sebres on 2019-07-31 19:59:35:

    1. comment changed to:
      The following script causes a segfault in DoRead:
      <verbatim>
      proc nexec {cmd in out stderr} {
          lassign [chan pipe] in1 out1
          lassign [chan pipe] in2 out2
          lassign [chan pipe] in3 out3
          puts [exec {*}$cmd >@ $out2 2>@ $out3 <@ $in1 &] 
          fileevent $in2 readable [list chan copy $in2 $out]
          fileevent $in3 readable [list chan copy $in3 $stderr]
          fileevent $out1 writable [list chan copy $in $out1]
      }
      
      proc test {args} {
        puts $args 
        set rest [lassign $args mode chan]
        switch -exact $mode {
          read {puts $chan "Test" ; close $chan}
          initialize {return "initialize watch finalize read write"}
          finalize {}
        }
      }
      
      set c [chan create "read write" test]
      
      if {$::tcl_platform(platform) ne "windows"} {
        nexec {sh -c "echo test"} $c $c $c
      } else {
        nexec {cmd /c "echo test"} $c $c $c
      }
      
      vwait forever
      
      </verbatim>
      
      Gives with a debug build:
      
      <verbatim>
      % source exec-test.tcl
      initialize rc0 {read write}
      36514
      read rc0 4096
      write rc0 {Test
      }
      finalize rc0
      Assertion failed: (bufPtr != NULL), function DoRead, file /Users/mpcjanssen/Src/tcltk/tcl/generic/tclIO.c, line 9815.
      </verbatim>
      
    2. icomment:
      Same on windows (I updated the ticket) - segfaulting too.
      
      Because it may be affected by buffer is still in use, [32ae34e63a] could be related issue (then it affects 8.5 in the same manner).
      
    3. login: "sebres"
    4. mimetype: "text/x-fossil-wiki"
  3. Ticket change [f68ef88491] (rid 225403) by apnadkarni on 2024-04-29 06:10:33:

    1. icomment:
      For the record, this bug is still present in Tcl 9.0b2.
      
      Looking at the core dump, the root cause seems similar to that in [88aef05cd](https://core.tcl-lang.org/tcl/tktview/88aef05cda36aca8233a) - reentrancy into TclInvokeMethod. A cursory inspection of the code shows the refchan read method in the test calling close which nests a call into finalize releasing various resources. As the stack unwinds at the top level TclInvokeMethod, freed memory is accessed resulting in the crash.
      
      Fixing would require either
      
      1. Protection of all data structures to handle reentrant calls
      
      2. Forbid any recursive calls into refchans from the script level refchan as suggested by @aku in the above mentioned ticket as one of the options.
      
      The former feels a daunting task for 9.0, though I'm not sure exactly how much work is required. The latter is likely simpler (though not trivial) but would mean backward incompatibility (implying it's either done for 9.0 or have to wait till 10.0), loss of function, and would require a TIP.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
    4. severity changed to: "Important"
  4. Ticket change [3a6124e6fc] (rid 225893) by sebres on 2024-05-14 15:43:50:

    1. icomment:
      Fix can be simpler: protect csPtr (StatePtr->csPtrR & StatePtr->csPtrW) by the refCount, so StopCopy would only free csPtr if its refCount gets 0.
      
      I think I already have a branch having such protection. Looking...
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  5. Ticket change [4837f27ee4] (rid 226007) by apnadkarni on 2024-05-14 16:14:29:

    1. icomment:
      Ref counting csPtr was the very first thing that had come to my mind. Then I saw dgp had already attempted a fix on similar basis and then backed it out. See his comments in https://core.tcl-lang.org/tcl/info/32ae34e63a and the code modifications in https://github.com/tcltk/tcl/commit/1f6b04d51b1c1270bf18e1535567f54c952a3158
      
      Perusing that code led me to realize the ref count based fixes, which were on very similar lines, were not as simple as I had initially imagined. That is where I concluded fixing for 9.0 would be a daunting task given the time frame.
      
      It would be great if you have a solution.
      
      Unfortunately, the issues that caused dgp to back out his fix do not seem to be listed anywhere.
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  6. Ticket change [75d2cc1df4] (rid 226028) by sebres on 2024-05-15 13:08:50:

    1. icomment:
      Here you go - in branch [fix-79474c58800cdf94](./timeline?r=fix-79474c58800cdf94) you'd find a [PR with a test and fix](./vdiff?from=79ebe65436469497&to=fix-79474c58800cdf94).
      
      And you was right, solely protection by ref-counting of csPtr doesn't always work (additionally it needs preserving of referenced r/w channels as well as small change in DoRead exiting with an error if no bufPtr is there). 
      Anyway it works as expected now (and I also did not see any leak there after implementation of ref-counter).
      
      I'll merge it later to 8.6 if no objections follow.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  7. Ticket change [c96fdb6910] (rid 226029) by oehhar on 2024-05-15 13:26:16:

    1. icomment:
      Sebres, Jan,
      great work!
      maybe, it may be tested by the continuous integration. Jan may advise, how to do this. Jan modifies a certain file to test a certain branch and it is tested over night...
      
      Take care,
      Harald
      
    2. login: "oehhar"
    3. mimetype: "text/x-fossil-wiki"
    4. username: "oehhar"
  8. Ticket change [31f7c23f57] (rid 226034) by jan.nijtmans on 2024-05-15 14:35:17:

    1. icomment:
      > Sebres, Jan, great work!
      
      I'll pass that to Ashok, I didn't do anything   ;-)
      
      Anyway, I added a tag such that it will be built on GITHUB, on 3 platforms and various configurations. We'll see the result tomorrow.
      
      Ashok, feel free to review. It looks good to me.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  9. Ticket change [bb469d4ad3] (rid 226046) by sebres on 2024-05-15 15:14:38:

    1. login: "sebres"
    2. mimetype: "text/plain"
    3. resolution changed to: "Fixed"
    4. status changed to: "Pending"
  10. Ticket change [3991f4e549] (rid 226047) by apnadkarni on 2024-05-15 15:15:14:

    1. icomment:
      I'll take a look, for my own edification more than anything else, but in a day or two... too busy with other things at the moment.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
    4. resolution changed to: "None"
    5. status changed to: "Open"
  11. Ticket change [8886932a95] (rid 226071) by sebres on 2024-05-16 09:17:05:

    1. assignee changed to: "apnadkarni"
    2. icomment:
      All CI flows [have passed](https://github.com/tcltk/tcl/commit/0ec4515a39296a85941e492191cad79ff56dc75a/status-details)... the latest changes are irrelevant for that, so I cancelled the ci-tag for now and set ticket to pending/fixed (and assigned to Ashok).
      
    3. login: "sebres"
    4. mimetype: "text/x-markdown"
    5. resolution changed to: "Fixed"
    6. status changed to: "Pending"
  12. Ticket change [2761b18997] (rid 226087) by dkf on 2024-05-16 12:45:05:

    1. icomment: "Looks **good to merge** to me."
    2. login: "dkf"
    3. mimetype: "text/x-markdown"
  13. Ticket change [063c17c79d] (rid 226088) by jan.nijtmans on 2024-05-16 12:54:02:

    1. icomment: "+1. Good work, Sergey!"
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  14. Ticket change [97d3ee25e1] (rid 226089) by apnadkarni on 2024-05-16 13:58:32:

    1. icomment:
      I see memory leak from valgrind. Have not reviewed the code yet, but my initial impression from the stack is that it is not from the fix but something that the fix has exposed in error handling.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  15. Ticket change [24539cb864] (rid 226092) by apnadkarni on 2024-05-16 14:17:31:

    1. icomment:
      Sorry, forgot to include the output
      
      ```
      make -s valgrind TESTFLAGS="-f ioCmd.test -match iocmd-32.3"
      ...
      ==52905== 6,728 (264 direct, 6,464 indirect) bytes in 1 blocks are definitely lost in loss record 115 of 115
      ==52905==    at 0x484280F: malloc (vg_replace_malloc.c:442)
      ==52905==    by 0x4894FA5: TclpAlloc (tclAlloc.c:699)
      ==52905==    by 0x48AE1C9: Tcl_Alloc (tclCkalloc.c:1059)
      ==52905==    by 0x49A2745: Tcl_CreateChannel (tclIO.c:1630)
      ==52905==    by 0x49B332E: TclChanCreateObjCmd (tclIORChan.c:665)
      ==52905==    by 0x48A0C55: Dispatch (tclBasic.c:4503)
      ==52905==    by 0x48A0CDB: TclNRRunCallbacks (tclBasic.c:4539)
      ==52905==    by 0x48A0594: Tcl_EvalObjv (tclBasic.c:4262)
      ==52905==    by 0x48A2A7D: TclEvalEx (tclBasic.c:5408)
      ==52905==    by 0x49BE690: Tcl_FSEvalFileEx (tclIOUtil.c:1821)
      ==52905==    by 0x49C99AB: Tcl_MainEx (tclMain.c:403)
      ==52905==    by 0x4012F4: main (tclAppInit.c:91)
      ==52905==
      ==52905== LEAK SUMMARY:
      ==52905==    definitely lost: 264 bytes in 1 blocks
      ==52905==    indirectly lost: 6,464 bytes in 131 blocks
      ==52905==      possibly lost: 0 bytes in 0 blocks
      ==52905==    still reachable: 0 bytes in 0 blocks
      ==52905==         suppressed: 0 bytes in 0 blocks
      ==52905==
      ```
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  16. Ticket change [540a9d5579] (rid 226094) by sebres on 2024-05-16 15:18:39:

    1. icomment:
      Hmm, indeed... I see 3 times `Tcl_Preserve` and only 2 times `Tcl_Release` for first `statePtr` (would correspond `chan create` from test)...<br/>
      (but there are definitely others - e. g. 2 times `Tcl_Preserve` and only 1 time `Tcl_Release` for the second `statePtr`).
      
      I'm also unsure it'd be caused by the fix directly, probably just introduced by possibility to do such things now. Investigating deeper.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  17. Ticket change [9ae5d38e3c] (rid 226114) by sebres on 2024-05-16 18:24:33:

    1. icomment:
      It looks like 1 object (03A09940) containing `rc0` (`$ch`) doesn't get decremented, so still hold a reference to statePtr (and therefore it is not released yet), whereas another object (03A09B08), also containing `rc0`, is removed.
      
      Here is a debug excerpt explaining that (3x preserve / 2x release of `statePtr` 03D933F0):
      ```
        ***==st allocat: 03D933F0 / Tcl_CreateChannel
        ***++st preserv: 03D933F0 / TclGetChannelFromObj === 03A09B08: rc0 ==
        ***++st preserv: 03D933F0 / TclGetChannelFromObj === 03A09940: rc0 ==
        ***++st preserv: 03D933F0 / Tcl_UnregisterChannel
        ***--st release: 03D933F0 / Tcl_UnregisterChannel
        ***--st release: 03D933F0 / FreeChannelInternalRep === 03A09B08
      ```
      
      Maybe it is something like ScriptRecord (script of chan event as list containing `$ch` as apply parameter) or some interim duplication (which then becomes a leak).
      However related the whole profiler output, all `CreateScriptRecord` are paired by `DeleteScriptRecord`.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  18. Ticket change [71ac8e1a09] (rid 226116) by sebres on 2024-05-16 19:19:10:

    1. icomment:
      OK, it looks like a `name` of reflected channel itself (the object is `rcPtr->name`, and after ref-decrement in `FreeReflectedChannel` its `refCount` is still 2, so has been not removed and hold the statePtr of channel within).
      
      
      The issue is therefore not related to this ticket and/or fix...<br/>
      It is simply a weird constellation: object containing the channel name (rc0) is referenced in reflected channel itself, and if the channel gets closed as long as its state is preserved several times (so FreeReflectedChannel doesn't remove it), a typical cyclic reference gets created:
      
      - `rcPtr->name` (Tcl_Obj of channel type) holds the ref to `statePtr`,
      - `statePtr` holds the ref to channel,
      - `chanPtr` holds the ref to reflected (`rcPtr` via `chanPtr->instanceData`),
      - `rcPtr` holds the ref to `rcPtr->name` (and cycle got closed).
      
      I'll try to solve that somehow as part of this fix.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  19. Ticket change [54d870750e] (rid 226121) by sebres on 2024-05-16 20:32:31:

    1. icomment:
      Commit [47cb98a686c4276b] should fix the leak.
      
      Tests and reviews are welcome (Ashok, could you please repeat your valgrind test again).
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  20. Ticket change [707f014dcb] (rid 226122) by apnadkarni on 2024-05-17 07:59:10:

    1. icomment:
      Not reviewed yet but there is still a leak - looks like a different one though, and again not related to your changes but exposed by them, I think.
      
      Only the tail of the log is below as it is quite large. Let me know if it would help to have the full log.
      
      ```
      repository:   d:\src\repos\tcl.fossil
      local-root:   c:/src/tcltk/wip/tcl/
      config-db:    C:/Users/apnad/AppData/Local/_fossil
      checkout:     47cb98a686c4276bab91492c7a31265ae9be56d6 2024-05-16 20:28:04 UTC
      ...
      ==5186== 6,440 (48 direct, 6,392 indirect) bytes in 1 blocks are definitely lost in loss record 113 of 113
      ==5186==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
      ==5186==    by 0x488CB64: TclpAlloc (tclAlloc.c:699)
      ==5186==    by 0x48A7273: Tcl_Alloc (tclCkalloc.c:1059)
      ==5186==    by 0x49D7CEA: Tcl_DuplicateObj (tclObj.c:1587)
      ==5186==    by 0x49F4AE2: Tcl_GetReturnOptions (tclResult.c:1588)
      ==5186==    by 0x49B6DC9: MarshallError (tclIORChan.c:997)
      ==5186==    by 0x49B8E4B: InvokeTclMethod (tclIORChan.c:2380)
      ==5186==    by 0x49B7566: ReflectInput (tclIORChan.c:1331)
      ==5186==    by 0x49A3E66: ChanRead (tclIO.c:425)
      ==5186==    by 0x49ABE48: GetInput (tclIO.c:6941)
      ==5186==    by 0x49B09C8: DoRead (tclIO.c:9955)
      ==5186==    by 0x49AFE46: CopyData (tclIO.c:9627)
      ==5186==    by 0x49AF35E: TclCopyChannel (tclIO.c:9294)
      ==5186==    by 0x49B58BF: Tcl_FcopyObjCmd (tclIOCmd.c:1719)
      ==5186==    by 0x4899156: Dispatch (tclBasic.c:4503)
      ==5186==    by 0x48991E0: TclNRRunCallbacks (tclBasic.c:4539)
      ==5186==    by 0x489BECD: TclEvalObjEx (tclBasic.c:6104)
      ==5186==    by 0x489BE5D: Tcl_EvalObjEx (tclBasic.c:6085)
      ==5186==    by 0x49AECAD: TclChannelEventScriptInvoker (tclIO.c:8996)
      ==5186==    by 0x49AE27A: Tcl_NotifyChannel (tclIO.c:8455)
      ==5186==    by 0x4A24AE6: FileWatchNotifyChannelWrapper (tclUnixChan.c:491)
      ==5186==    by 0x4A3233E: FileHandlerEventProc (tclUnixNotfy.c:808)
      ==5186==    by 0x49D6B71: Tcl_ServiceEvent (tclNotify.c:670)
      ==5186==    by 0x49D6FC1: Tcl_DoOneEvent (tclNotify.c:967)
      ==5186==
      ==5186== LEAK SUMMARY:
      ==5186==    definitely lost: 48 bytes in 1 blocks
      ==5186==    indirectly lost: 6,392 bytes in 129 blocks
      ==5186==      possibly lost: 0 bytes in 0 blocks
      ==5186==    still reachable: 0 bytes in 0 blocks
      ==5186==         suppressed: 0 bytes in 0 blocks
      ```
      
    2. login: "apnadkarni"
    3. mimetype: "text/x-markdown"
  21. Ticket change [1ad4020f73] (rid 226129) by sebres on 2024-05-17 10:22:42:

    1. icomment:
      Yep, and trying to find this one leak, I found that a write handler in test reflected command was incorrect - this didn't return an integer, thus produced an after effect error <code>{expected integer but got ""}</code> (that probably additionally caused the leak)...
      
      However fixing the test induced further segfault (and 2nd assert(bufPtr) throws then too).
      
      Commit [2c48f0c544ad9ddd] fixes this segfault now.
      
      I don't know whether it'd circumvent the leak (it was <code>resObj</code> marshalled from an error, probably because of wrong write return code), I rewrote the test in [0dac722930816ea3] to cover both variants (without and with an error in reflected write), although both variants seem to segfault without the fix.
      
      But I also think that this leak has nothing to do with the problem or the fix.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  22. Ticket change [c4561785a8] (rid 226138) by apnadkarni on 2024-05-17 11:29:10:

    1. icomment:
      Found the new reported leak. I think. As expected, not caused by your fixes. ChannelState gets freed via Tcl_EventuallyFreed(.., TCL_DYNAMIC) but that will not free ChannelState.{chanMsg,unreportedMsg}
      
      Only ran into this because of the error you mentioned in the write handler in the new test case.
      
      Testing now.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  23. Ticket change [f29fa4b208] (rid 226141) by sebres on 2024-05-17 12:14:22:

    1. icomment:
      Yes I already found it too... Checking now whether other members of statePtrr (besides chanMsg) can be affected in similar way.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  24. Ticket change [cb158b94f6] (rid 226142) by apnadkarni on 2024-05-17 12:41:58:

    1. icomment:
      Will leave it to you then. My fix is in apn-channelstate-leak. I preferred not to touch other fields for the reasons I mentioned in the comment. If you are confident they are appropriately null-ed when freed, then yes, it would be good to add them.
      
      Thanks for fixing this vexing crash.
      
      /Ashok
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  25. Ticket change [e061157b12] (rid 226146) by sebres on 2024-05-17 13:08:22:

    1. icomment:
      I integrated it in [f41c1304d5f124fe]... and added more clean-ups for other members that look safe and, I can imagine, they may be set after the close, asynchronously via some callbacks or handlers (e. g. synthetic timer scheduled by UpdateInterest, etc).
      
      Thanks for co-working, Ashok.
      
      @everyone, I'll leave it as it is for CI (added a tag to test)...<br/>
      Thx for review and test.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  26. Ticket change [4f3782e939] (rid 226188) by apnadkarni on 2024-05-19 07:54:42:

    1. icomment:
      Sergey, reviewed and tested on linux with valgrind, windows with iocp and twapi tls. Looks good to me to be merged.
      
    2. login: "apnadkarni"
    3. mimetype: "text/plain"
  27. Ticket change [d7a70aee95] (rid 226297) by sebres on 2024-05-21 09:03:16:

    1. icomment:
      All [tests have passed too](https://github.com/tcltk/tcl/commit/526da2b6e941e4eff22c7209ae4445ea2eaf97bd/status-details)... So I'll merge it to mainline branches.
      
    2. login: "sebres"
    3. mimetype: "text/x-markdown"
  28. Ticket change [491ce9ae63] (rid 226317) by jan.nijtmans on 2024-05-21 10:22:20:

    1. icomment:
      You'll notice that core-8-branch is currently in a non-stable state, that's why the latest change is not merged yet to trunk. This creates a problem for you, when trying to merge your change to trunk: it will introduce the same problem in trunk.
      
      Therefore, I suggest to "cherry-pick" your fix to trunk. Hopefully dkf will solve his problem soon, but that shouldn't stop you.
      
    2. login: "jan.nijtmans"
    3. mimetype: "text/x-fossil-wiki"
  29. Ticket change [e7dcec90ce] (rid 226323) by sebres on 2024-05-21 10:35:31:

    1. closedate changed to: "2460451.94133481"
    2. closer changed to: "sebres"
    3. icomment: "merged, so closed now."
    4. login: "sebres"
    5. mimetype: "text/plain"
    6. status changed to: "Closed"