Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-25
12:59
Merge [7d3bfe817b052981]: Fix [bda99f2393]. Windows non-blocking gets stdin truncation. See ticket f... check-in: c2ce887fb7 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
12:50
Merge [b2d1898cb967ec1c]: Fix EOF test for TIP 646. Add tests for Bug [bda99f2393]. Remove Scriptics... check-in: e78f391fc8 user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-05-08
15:03 • Ticket [bda99f2393] gets stdin problem when non-blocking - Windows status st... artifact: 0ebc83e12f user: jan.nijtmans
2024-05-07
16:06 • Ticket [bda99f2393]: 5 changes artifact: d2c034d0c4 user: apnadkarni
15:21 • Ticket [bda99f2393]: 5 changes artifact: a39512fbfd user: jan.nijtmans
2024-03-19
16:12
Backport fix for [bda99f2393]: gets stdin problem when non-blocking - Windows (cherry-pick): Fix ind... check-in: 770221ff54 user: jan.nijtmans tags: core-8-branch
2024-02-26
05:48 • Closed ticket [bda99f2393]: gets stdin problem when non-blocking - Windows p... artifact: e45372a594 user: apnadkarni
05:43
Fix [bda99f2393]. Windows non-blocking gets stdin truncation. See ticket for resolution. check-in: 7d3bfe817b user: apnadkarni tags: trunk, main
2024-02-23
07:33 • Ticket [bda99f2393] gets stdin problem when non-blocking - Windows status st... artifact: 79ad6c05e1 user: apnadkarni
2024-02-20
06:19 • Ticket [bda99f2393]: 3 changes artifact: 6848c98d13 user: apnadkarni
05:56
Merge main. Enable [bda99f2393] tests. closed check-in: d3e82ccfc5 user: apnadkarni tags: apn-bug-bda99f2393
05:47
Fix EOF test for TIP 646. Add tests for Bug [bda99f2393]. Remove Scriptics copyright (none of their ... check-in: b2d1898cb9 user: apnadkarni tags: trunk, main
2024-02-19
16:58
Starton [bda99f2393]. check-in: 8c77a9a3c5 user: apnadkarni tags: apn-bug-bda99f2393
12:39 • Ticket [bda99f2393] gets stdin problem when non-blocking - Windows status st... artifact: b07406a246 user: apnadkarni
11:09 • Ticket [bda99f2393]: 3 changes artifact: e4acfe6c97 user: apnadkarni
06:35 • New ticket [bda99f2393]. artifact: 0ef6a0e73d user: juliannoble2

gets stdin problem when non-blocking - Windows

Closed / Fixedbda99f23936f7170e8f51e7e3eb52db794a3195cCreated 3 years agoUpdated 3 years ago
Descriptionplain2024-02-19 06:35:26
Seems to affect Windows only. (tested 8.7a6 and 9.0b in ubuntu under WSL and not affected, reported by stevel as not a problem on macos)

when stdin is in line mode but the channel is configured with -blocking 0 - gets fails to return a line when input is above 100 characters.
The state of -translation auto/cr/lf/crlf doesn't seem to make any difference.
(chan pending stdin input will show there are bytes available after hitting enter - but gets returns -1)

This is most easily seen in a tclsh shell which behaves differently on windows Tcl 8.7a6/9.0b compared to 8.7a5 and earlier, and is different to Tcl on other platforms.

The string repeat is just to show line of the right length.
The first set v line is just shy of 100 chars of input - and hitting return brings you straight to the prompt as expected.
The second set v appears to hang after enter is pushed, but hitting enter a second time, or any other key will return to the prompt.
If enter was pushed, you get a double prompt. If another key was pushed - the repl prepends that character to the next commandline entered  - usually resulting in an invalid command error.
(although the extra character is only visible above the % on the previous line or to the left of the % depending on which terminal is being used)

While running the standard repl with stdin set to line mode and nonblocking might not be usual - this behaviour of gets and stdin is potentially confusing/a source of bugs in other contexts.

    %chan conf stdin -blocking 0
    % string repeat a 102
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    % set v aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    % set v aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    % %
Referenced by7
apnadkarni Ticket [bda99f2393]: 3 changes [History diff]2024-02-19 11:09:50
apnadkarniplain2024-02-19 12:39:54
8.7/9.0 console driver uses a temporary buffer size of 200 (100 chars) while 8.6 uses a buffer size of 8192. The simple immediate "fix" seems to be to bump up the 9.0 buffer size (only one per process) to 8192 as in 8.6. However, I suspect that is just "postponing" the problem to entering 8192/2 characters so need to investigate further.

In theory, the reads are supposed to read in buffer size chunks (e.g. 200 as of now) and pass them to Tcl piecemeal so the buffer size should not matter. Investigating further...
apnadkarnimarkdown2024-02-20 06:19:40

There were two issues. Recording for posterity.

First, PeekConsoleInput was being used to check if there is input. It turns out this Win32 API only checks the console input records that have not yet been converted to text. So for example in the bug report, if the entered line was 110 characters, the ReadConsole with a buffer size of 200 (100 characters) would return 100 characters and store the remaining 10 internally for the next read. The console's input record queue would be empty so when the Tcl console driver called PeekConsoleInput, it would report no input pending. Entering another character would create a input record, thereby having the Peek indicate input and triggering a read. To fix this, the Tcl console read thread will now immediately trigger another read if the previous read completely filled the passed buffer. Tested with manually reducing read buffer size down to 20 bytes (since the fix below requires increasing buffer size for a different reason).

The second issue, not reported above but mentioned on the chat is the limit of 254 characters on input lines. It seems ReadConsole will limit line lengths to the max of 256 (including crlf) and the supplied buffer size (easily checked via a simple console program). There does not seem any way around this so now the buffer size has been bumped back to 8192 as in 8.6. Note this is a single allocation per process in 9.0 as opposed to per thread in 8.6, so impact should be minimal.

apnadkarniplain2024-02-23 07:33:43
Julian,

Could you try the code in the apn-bug-bda99f2393 branch to see if it fixes your reported issues? Also, any other issues that might show up with your in-development custom console?

/Ashok
apnadkarniplain2024-02-26 05:48:56
Purportedly fixed in [7d3bfe817b].
jan.nijtmanswiki2024-05-07 15:21:24

Should this be backported to 8.6 and 8.7 as well?

apnadkarniplain2024-05-07 16:06:21
8.6 uses a different architecture (3 stdio threads per Tcl thread). Not affected by this.

8.7 is affected.

/Ashok
jan.nijtmanswiki2024-05-08 15:03:26

I forgot: Already backported to 8.7 [here]

Log in (anonymous is fine) to add a comment.