Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-10-12
19:52 • Closed ticket [ba8845d272]: zipfs global tables leak plus 4 other changes artifact: 596640454d user: serhiy.storchaka
2024-08-22
20:53
Merge [6afcffcb660971ef]: Bug [33b2486199] - thread create/release unmounts zipfs. check-in: dfc86cdbd2 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2023-11-03
16:51 • Ticket [33b2486199] zipfs becomes non-functional after first thread finalization... artifact: f207ae716b user: griffin
16:16 • Closed ticket [33b2486199]. artifact: a2a55ca3b0 user: dgp
15:21 • Ticket [33b2486199]: 3 changes artifact: e829150e78 user: dgp
15:11 • Ticket [33b2486199]: 3 changes artifact: 406b8f53d8 user: dgp
13:49 • Open ticket [33b2486199]. artifact: 3a3e42f2fd user: dgp
2023-10-23
13:12 • Closed ticket [33b2486199]. artifact: acf462729c user: dgp
13:12 • Ticket [33b2486199]: 3 changes artifact: 0ccc307f1e user: dgp
2023-10-21
15:07 • Pending ticket [33b2486199]. artifact: 1808dc7500 user: apnadkarni
06:09
Merge 8.7 - Bug [33b2486199] - thread create/release unmounts zipfs check-in: 6afcffcb66 user: apnadkarni tags: trunk, main
05:48
Bug [33b2486199] - thread create/release unmounts zipfs check-in: 7eaba2ba2f user: apnadkarni tags: core-8-branch
04:45 • Ticket [33b2486199] zipfs becomes non-functional after first thread finalization... artifact: 59ece3a8b5 user: apnadkarni
2023-10-20
17:52 • Ticket [33b2486199]: 3 changes artifact: 8a90cb41aa user: apnadkarni
17:35 • Ticket [33b2486199]: 3 changes artifact: 662ae3c384 user: apnadkarni
16:15 • Ticket [33b2486199]: 6 changes artifact: 03f245de39 user: dgp
14:35 • Ticket [33b2486199]: 3 changes artifact: 0aa5e060f7 user: dgp
2023-10-19
16:30 • Ticket [33b2486199]: 3 changes artifact: 666dc29715 user: apnadkarni
2023-10-18
16:08 • Ticket [33b2486199]: 5 changes artifact: 1c6b4c360b user: dgp
2023-10-16
21:48 • Ticket [33b2486199]: 3 changes artifact: 23d5887d27 user: dgp
21:46 • New ticket [33b2486199]. artifact: 7de532c225 user: dgp

zipfs becomes non-functional after first thread finalization

Closed / Fixed33b248619922b8781ab500cd778f177fedf3ebe8Created 3 years agoUpdated 3 years ago
Descriptionplain2023-10-16 21:46:06
Using Tcl's tip of core-8-branch and Thread's tip of thread-2-8-branch:

$ make test-tcl TESTFLAGS='-singleproc 1 -file "chanio.test clock.test" -match "chan-io-70.1 clock-58.1" -verbose bpste'
...
chanio.test
---- chan-io-70.1 start
++++ chan-io-70.1 PASSED
clock.test
---- clock-58.1 start


==== clock-58.1 clock l10n - Japanese localisation FAILED
==== Contents of test case:

        set trouble {}
        foreach {date jdate} {
            1872-12-31 西暦1872年12月31日
            1873-01-01 明治06年01月01日
            1912-07-29 明治45年07月29日
            1912-07-30 大正01年07月30日
            1926-12-24 大正15年12月24日
            1926-12-25 昭和01年12月25日
            1989-01-07 昭和64年01月07日
            1989-01-08 平成01年01月08日
            2019-04-30 平成31年04月30日
            2019-05-01 令和01年05月01日
        } {
            set status [catch {
                set secs [clock scan $date  -timezone +0900  -locale ja_JP  -format %Y-%m-%d]
                set jda [clock format $secs  -timezone +0900  -locale ja_JP  -format %Ex]
            } result]
            if {$status != 0} {
                append trouble \n $date " gives error " $result
            } elseif {$jda ne $jdate} {
                append trouble \n $date " converts to "  [backslashify $jda] " and should be "  [backslashify $jdate]
            }
            # There is no code for scanning dates on the locale's
            # alternative calendar.
            continue
            set status [catch {
                set secs [clock scan $jdate  -timezone +0900  -locale ja_JP  -format %Ex]
                set da [clock format $secs  -timezone +0900  -locale ja_JP  -format %Y-%m-%d]
            } result]
            if {$status != 0} {
                append trouble \n [backslashify $jdate] " gives error " $result
            } elseif {$da ne $date} {
                append trouble \n [backslashify $jdate] " converts to "  $da " and should be " $date
            }
        }
        set trouble
    
---- Result was:
\x0A1872-12-31 converts to 12/31/1872 and should be \u897f\u66a61872\u5e7412\u670831\u65e5\x0A1873-01-01 converts to 01/01/1873 and should be \u660e\u6cbb06\u5e7401\u670801\u65e5\x0A1912-07-29 converts to 07/29/1912 and should be \u660e\u6cbb45\u5e7407\u670829\u65e5\x0A1912-07-30 converts to 07/30/1912 and should be \u5927\u6b6301\u5e7407\u670830\u65e5\x0A1926-12-24 converts to 12/24/1926 and should be \u5927\u6b6315\u5e7412\u670824\u65e5\x0A1926-12-25 converts to 12/25/1926 and should be \u662d\u548c01\u5e7412\u670825\u65e5\x0A1989-01-07 converts to 01/07/1989 and should be \u662d\u548c64\u5e7401\u670807\u65e5\x0A1989-01-08 converts to 01/08/1989 and should be \u5e73\u621001\u5e7401\u670808\u65e5\x0A2019-04-30 converts to 04/30/2019 and should be \u5e73\u621031\u5e7404\u670830\u65e5\x0A2019-05-01 converts to 05/01/2019 and should be \u4ee4\u548c01\u5e7405\u670801\u65e5
---- Result should have been (exact matching):

==== clock-58.1 FAILED
Referenced by4
dgpplain2023-10-16 21:48:35
Taking a very lightly educated guess, this might be the kind of
trouble that shows up when [clock] can't find its message catalogs?

So how could exercising some thread-enabled channel actions cause
some inability to find and load message catalogs from the zipfs archive?

Hope others can reproduce.  I see it on two systems.
dgpplain2023-10-18 16:08:33
Looks like something broken in Thread.

$ make shell
...
% package require Thread
2.8.9
% file system //zipfs:/app/
zipfs zip
% thread::release [thread::create]
0
% file system //zipfs:/app/
native
apnadkarniplain2023-10-19 16:30:15
I hope to look into this over the weekend. As also the pending question of why your builds were pulling in corrupted encoding files into the constructed zip attachment and not the ones from the source directory.
dgpplain2023-10-20 14:35:39
The outdated *.enc files were never going into the zipfs.

They were brought into the interpreter because the installation
directory where they were was first on the [encoding dirs] search
path.
dgpplain2023-10-20 16:15:08
It appears that every thread finalization leads to
a call to TclZipfsFinalize() within the thread getting
finalized.

That routine, though, doesn't seem to take any notice
of which thread it is operating in, and unconditionally
performs what looks like a  global cleanup.

This means the first finalization of any thread, disables
and shuts down the zipfs subsystem, or so it appears.

Unless I'm missing something.

I'm really surprised such a fundamental flaw could go
unnoticed for so long.
apnadkarniplain2023-10-20 17:35:50
Potential (simple) fix in branch bug-33b2486199.

I'd prefer someone more knowledgeable in Tcl's IO/file system finalization to review.

The fundamental problem is that there is currently no bookkeeping that I can see of which thread mounted the archive and which are still using it. Normally there would be a mount/unmount pair but since this Tcl library zip mount is implicit, it needs to be present till process exit (presumably).

So I've simply added a check there for TclInExit. Not sure that is the right or complete solution but if no one has anything better to offer, I'll merge it in at some point.
apnadkarniplain2023-10-20 17:52:04
Thinking about it a bit more, I think finalization only on TclInExit() (without explicit bookkeeping of threads) is the right solution in any case. Even after the last current Tcl thread exits, a C application could still create a new thread which would need access to the library mount so it needs to stay in place until process exit.

Then again, as I said, I don't understand Tcl's initialization sequence and the above may not be true.

Perhaps the right approach is to add a "Cleanup" method to the file system implementation dispatch table that gets invoked by Tcl at the "right" time. If zipfs was implemented as an extension the finalization call as currently directly invoked from TclFinalizeIOSubsystem would not be possible.
apnadkarniplain2023-10-21 04:45:37
I've moved the ZipFSFinalize call to TclFinalizeFilesystems which I think is the correct place as it is called from Tcl_Finalize on process exit.

The loop in TclFinalizeFilesystems that frees each file system in turn would be the right place to add a call to a Tcl_Filesystem.finalize method except such Tcl_Filesystem (surprisingly) does not have such a method slot defined for file systems to do their cleanup. I'm not inclined to write a TIP so hardcoded for now.
apnadkarniplain2023-10-21 15:07:26
Please retest. Should be fixed.
dgpplain2023-10-23 13:12:32
The core-8-branch is now testing successfully for me.  Thanks!
dgp Closed ticket [33b2486199]. [History diff]2023-10-23 13:12:52
dgpplain2023-11-03 13:49:45
Seems this was only a partial fix.

$ ./tcltest ../tests/zipfs.test
zipfs.test:     Total   508     Passed  473     Skipped 35      Failed  0
Number of tests skipped for each constraint:
        23      !zipfslib
        1       bug-4ae42446ab
        11      win

That is indeed a successful test.  But....

$ ./tclsh ../tests/zipfs.test


==== bug-33b2486199 zipfs unmounted on thread exit FAILED
==== Contents of test case:

        set before [lsort [zipfs mount]]
        thread::release [thread::create]
        after 100; # Needed to allow the spawned thread to exit to trigger bug
        string equal $before [lsort [zipfs mount]]
    
---- Result was:
0
---- Result should have been (exact matching):
1
==== bug-33b2486199 FAILED

zipfs.test:     Total   508     Passed  472     Skipped 35      Failed  1
Number of tests skipped for each constraint:
        23      !zipfslib
        1       bug-4ae42446ab
        11      win

Some probing of the failing test shows that all zipfs mounts disappear.

It's very tricky tracing through all the configure/build combinatorics
in the zipfs tangle.
dgpplain2023-11-03 15:11:09
Sorry, I think that's a false alarm.
dgpplain2023-11-03 15:21:39
So this is a very old problem that continues to snag me from time to time.

If I do a default build on unix -- that is a --enable-shared build -- and I have previously installed an earlier build, then any tests I run will load
the installed Tcl library and not the just-built one and I get test results
from the old library, often a version before some important bug fix like
in this case.

This is why my normal development practice is to always perform
--disable-shared builds so every `make test` actually tests what I am
working on.

And that's fine for a lot of development work, but when the two
configurations potentially do not behave the same, it really gets in
the way.

Sometimes `make shell` is supposed to be a tool to avoid the problem,
but that is not working.
dgp Closed ticket [33b2486199]. [History diff]2023-11-03 16:16:38
griffinplain2023-11-03 16:51:58
I don't know if this would solve the problem or not:  I was just thinking this morning that the "stand-alone" static tclsh should have a different name, and be part of any install.  The current state of having to use --disable-shared to get the stand-alone variant, and not being able to (obviously) identify an installation as such has tripped me up several times.  

This means "tclsh" and "tclkitsh" (for example) would reside together in bin, being built together regardless of shard-ness option selection.

I have not thought deeply about the ramifications of such a change.
Log in (anonymous is fine) to add a comment.