Add a test to cover the behavior described in https://github.com/bitcoin/bitcoin/issues/2039, in preparation for fixing it.
I massaged the functional test: fewer levels of try-except wrapping, deduplication of the platform specific code (only the command name changes based on the OS)
This is a backport of core#28545, core#27850 and core#28660
Depends on D18969