Severe Problems With Command 'sync'

The current implementation has severe problems, so it has been completely overworked.

Note that these tests are the results from the March (2016) version of wish8.6.5 (the repository of Tk doesn't provide revision numbers – this is a step backwards). In the meanwhile the first bug has been fixed (and probably some more when you read this).

Here is the first example for a crash (but fixed in newer repository versions):

A crash with nested calls:

A crash with succeeding calls:

Now try this script:

The result is "1", with other words: procedure Sync has been called although the line height calculation is not up-to-date at this moment. This is very confusing, and should not happen at all.

Another problem in original implementation: the following script hangs (often, sometimes it doesn't), but it works as expected in the revised implementation (all other problems are also eliminated):

Also the following script hangs with original implementation: updateAction will not be called, although it should be called due to documentation:

And also in this script sync is not working at all:

The output is "height=18", this result is incorrect, because it has to wrap into many display lines. And it terminates with error message can't read "tk_textInvalidateLine", this is possible only if no display line calculation has been done at all (because the line metric calculation is defining this variable, if debug mode is enabled).

The revised implementation now triggers the <<WidgetViewSync>> event even when sync is returning immediately since there is no pending update:

The updated documentation:

pathName sync ?-command ?command??

Controls the synchronization of the view of the text widget.

pathName sync
Immediately brings the line metrics up-to-date by forcing computation of any outdated line heights. The command returns immediately if there is no such outdated line heights, otherwise it returns only at the end of the computation. It is guaranteed that the <<WidgetViewSync>> virtual event fires after executing this command, even if no line was outdated. This command returns an empty string.

pathName sync -command
Returns the command which will be scheduled as soon an the line metric calculation is up-to-date, or an empty string if no command is available.

pathName sync -command command
Schedules command to be executed (by the event loop) exactly once as soon as all line heights are up-to-date. A new script will replace any existing script, but if the first character is “+” then the new script augments an existing script. If there is no pending line metrics computation, the scheduling is immediate (as if the line metrics computation will finish now). bgerror is called on command failure. This command returns an empty string.

If a command is given (with -command command) then it is ensured that command will be called only when all line height values currently are up-to-date. When waiting for event <<WidgetViewSync>> it is possible that some race conditions will affect the received state.

I want to state that this definition is still compatible to original definition; the original documentation of sync is very sparse. And the extension with augmenting a script is compatible because the original implemented is crashing in this case. I cannot understand why the original implementation is not offering this standard Tk behavior with replacing and augmenting.

A note about the -update option in command count, the current documentation says:

This -update option is obsoleted by pathName sync, pathName pendingsync and <<WidgetViewSync>>.

But this is not true, there is a big difference. sync waits for the syncnronization of all display lines, but count -update only needs the synchronization within the lines of the specified range, and the revised implementation (and also the original implementation) is actually doing this. Moreover the problem with race conditions does not occur with count -update. With other words, option -update isn't obsolete at all.