When signing XML files using a smart card the latter will be reset by Windows when the signing process takes more than 5 seconds. This issue exists only on Windows 8+.
The problem is that the key store gets initialized before the digest values are computed. If this calculation takes more than 5 seconds Windows resets the smart card because of an inactive transaction. After the calculations are done and the actual signing process starts the smartcard is no more available and the task fails.
The reset is logged by Windows an listed in Event Viewer as a warning with source Smart Card Service: "The card in Smart Card Reader 'ACS CCID USB Reader 0' has been reset because an application held an exclusive transaction on the card for 5 seconds without activity. If this error persists, the application may not be functioning correctly."
This behavior is documented in https://msdn.microsoft.com/en-us/library/windows/desktop/aa379469%28v=vs.85%29.aspx
The solution is quite simple. The order of calculating the digest values and initializing the key store has to be changed.
Testing however requires a Windows 8 or 10 machine (or vm), a smart card and a fairly large XML file.
The patch to be applied: