Audacity does not play last little bit of sound

I am using Audacity 2.3.3 on Kubuntu 20.04 from the repository, but I am not sure that this problem is specific to either the version of Audacity or Linux.

I found the same problem described in the following two forum posts (the first references the second):

As mentioned there, when Audacity plays back a sound, it cuts off just before the last fraction of a second. Playing the same clip in another app (e.g. VLC) plays the whole thing to the end. This makes it very annoying for editing and exporting clips in Audacity where you need to cut the sound in the middle of bits of speech.

The solution suggested there is to install and run Jack, but as I was trying this and failing, it occurred to me that if other apps like VLC can play the sound to the end, then the problem must be with Audacity and not with the sound server.

Moreover, this must be a bug that has plagued Audacity for years as I recall having this issue long ago, and those other forum posts are 8 and 9 years old respectively.

I think that the problem is that Audacity has a rather unique way of handling audio data streams.

Most audio applications connect to the sound system when the application is launched, and maintain that connection until the application exits. Audacity does not do that. Audacity only makes a connection when play and/or record are started, and disconnect on stop. This was necessary back in the days of ALSA only sound systems (and is still necessary for anyone running ALSA only for their sound system), because ALSA usually only allows one application at a time to access the audio device (“exclusive” access).

The problem is that on stop, the last bit of audio is still held in sample buffers.

To minimise the problem, try decreasing Audacity’s buffer setting in “Edit > Preferences > Devices: → Buffer Length”.
Note that if the buffer is set too small, playback is likely to become glitchy or not work at all. The default buffer length is 100 ms, which is quite generous for most machines. On a modern fast machine, you can probably drop it down as low as 10 ms.