|
2026-10-12
| ||
| 19:22 | • Closed ticket [32ae34e63a]: chan copy reflected channel segfault plus 4 ot... artifact: 4d0603f507 user: serhiy.storchaka | |
|
2024-08-28
| ||
| 17:06 | Merge [bc2e65239403f994]: Fix [79474c58800cdf94]: 2 segfaults and 2 leaks (common IO handlers and re... check-in: b40703dc9e user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE | |
|
2024-05-21
| ||
| 10:35 | • Closed ticket [79474c5880]: refchan segfault plus 5 other changes artifact: e7dcec90ce user: sebres | |
| 10:22 | • Ticket [79474c5880]: 3 changes artifact: 491ce9ae63 user: jan.nijtmans | |
| 09:45 | closes [79474c58800cdf94]: fixes 2 segfaults and 2 leaks (common IO handlers and reflected channels) check-in: 4342b27a4b user: sebres tags: core-8-6-branch | |
| 09:03 | • Ticket [79474c5880] refchan segfault status still Pending with 3 other chang... artifact: d7a70aee95 user: sebres | |
|
2024-05-19
| ||
| 07:54 | • Ticket [79474c5880]: 3 changes artifact: 4f3782e939 user: apnadkarni | |
|
2024-05-17
| ||
| 13:08 | • Ticket [79474c5880]: 3 changes artifact: e061157b12 user: sebres | |
| 12:41 | • Ticket [79474c5880]: 3 changes artifact: cb158b94f6 user: apnadkarni | |
| 12:14 | • Ticket [79474c5880]: 3 changes artifact: f29fa4b208 user: sebres | |
| 11:29 | • Ticket [79474c5880]: 3 changes artifact: c4561785a8 user: apnadkarni | |
| 10:22 | • Ticket [79474c5880]: 3 changes artifact: 1ad4020f73 user: sebres | |
| 07:59 | • Ticket [79474c5880]: 3 changes artifact: 707f014dcb user: apnadkarni | |
|
2024-05-16
| ||
| 20:32 | • Ticket [79474c5880]: 3 changes artifact: 54d870750e user: sebres | |
| 20:28 | fix mem-leak originating by cyclic reference `rcPtr->name (type "channel", its refCount may be large... check-in: 47cb98a686 user: sebres tags: fix-79474c58800cdf94 | |
| 19:19 | • Ticket [79474c5880] refchan segfault status still Pending with 3 other chang... artifact: 71ac8e1a09 user: sebres | |
| 18:24 | • Ticket [79474c5880]: 3 changes artifact: 9ae5d38e3c user: sebres | |
| 15:18 | • Ticket [79474c5880]: 3 changes artifact: 540a9d5579 user: sebres | |
| 14:17 | • Ticket [79474c5880]: 3 changes artifact: 24539cb864 user: apnadkarni | |
| 13:58 | • Ticket [79474c5880]: 3 changes artifact: 97d3ee25e1 user: apnadkarni | |
| 12:54 | • Ticket [79474c5880]: 3 changes artifact: 063c17c79d user: jan.nijtmans | |
| 12:45 | • Ticket [79474c5880]: 3 changes artifact: 2761b18997 user: dkf | |
| 09:17 | • Pending ticket [79474c5880]. artifact: 8886932a95 user: sebres | |
|
2024-05-15
| ||
| 15:15 | • Open ticket [79474c5880]. artifact: 3991f4e549 user: apnadkarni | |
| 15:14 | • Pending ticket [79474c5880]. artifact: bb469d4ad3 user: sebres | |
| 14:35 | • Ticket [79474c5880]: 3 changes artifact: 31f7c23f57 user: jan.nijtmans | |
| 13:26 | • Ticket [79474c5880]: 4 changes artifact: c96fdb6910 user: oehhar | |
| 13:08 | • Ticket [79474c5880]: 3 changes artifact: 75d2cc1df4 user: sebres | |
| 12:52 | ultimate fix for [79474c58800cdf94]: avoid segfault on copy-state structure freed to early, protecte... check-in: b66be298b9 user: sebres tags: fix-79474c58800cdf94 | |
| 12:46 | test illustrating bug [79474c58800cdf94] (segfaults at end of iocmd-32.3) check-in: b2573c10f0 user: sebres tags: fix-79474c58800cdf94 | |
| 12:39 | ultimate fix for [79474c58800cdf94]: avoid segfault on copy-state structure freed to early, protecte... check-in: 8ca3b56334 user: sebres tags: fix-79474c58800cdf94--8-5-based | |
|
2024-05-14
| ||
| 16:14 | • Ticket [79474c5880] refchan segfault status still Open with 3 other changes artifact: 4837f27ee4 user: apnadkarni | |
| 15:43 | • Ticket [79474c5880]: 3 changes artifact: 3a6124e6fc user: sebres | |
|
2024-04-29
| ||
| 06:10 | • Ticket [79474c5880]: 4 changes artifact: f68ef88491 user: apnadkarni | |
|
2019-07-31
| ||
| 19:59 | • Ticket [79474c5880]: 4 changes artifact: 1622194e55 user: sebres | |
| 13:22 | • New ticket [79474c5880]. artifact: 336d9452b4 user: mjanssen | |
refchan segfault
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
Gives with a debug build:
% 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.
4d0603f507Closed ticket [32ae34e63a]: [chan copy] reflected channel segfault plus 4 other changes ticket2026-10-12 19:22:40
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).
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 - 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
Protection of all data structures to handle reentrant calls
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.
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...
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.
Here you go - in branch fix-79474c58800cdf94 you'd find a PR with a test and fix.
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.
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
> 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.
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.
All CI flows have passed... 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).
Looks good to merge to me.
+1. Good work, Sergey!
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.
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==
Hmm, indeed... I see 3 times Tcl_Preserve and only 2 times Tcl_Release for first statePtr (would correspond chan create from test)...
(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.
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.
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...
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 tostatePtr,statePtrholds the ref to channel,chanPtrholds the ref to reflected (rcPtrviachanPtr->instanceData),rcPtrholds the ref torcPtr->name(and cycle got closed).
I'll try to solve that somehow as part of this fix.
Commit [47cb98a686c4276b] should fix the leak.
Tests and reviews are welcome (Ashok, could you please repeat your valgrind test again).
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
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 {expected integer but got ""} (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 resObj 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.
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.
Yes I already found it too... Checking now whether other members of statePtrr (besides chanMsg) can be affected in similar way.
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
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)...
Thx for review and test.
Sergey, reviewed and tested on linux with valgrind, windows with iocp and twapi tls. Looks good to me to be merged.
All tests have passed too... So I'll merge it to mainline branches.
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.
merged, so closed now.