Submit invalid block when solution is found right after receiving new block template
**Describe the bug**
We are using Xmrig to merge mine Monero with Tari so we have a proxy sitting between Xmrig and a Monero Daemon.

We have observed a very intermittent bug where Xmrig is working on a block N and then when it calls `get_block_template` the Monero daemon gives it a new template for block N+1 a few milliseconds before one of the mining worker threads finds a valid solution for block N. When Xmrig then calls `submit_block` for this solution it submits the solution nonce with the newly received block N+1 template instead of block N which is was working on. This results in that solution being rejected.

My intuition is that there is a timing issue between receiving a new template, updating the mining workers and how the solution found event is combined with the current template before calling `submit_block`

**To Reproduce**
Tricky to reproduce as it seems to occur when `get_block_template` is called just before a mining worker finds a valid solution. I observe it once a day maybe on a single CPU miner.

**Expected behavior**
When a solution is found for block N it submits the nonce with block template N not block template N+1.

**Required data**
![image](https://user-images.githubusercontent.com/25514413/135229928-5681b613-1e9c-4b40-a6bc-e69a6a42c6ff.png)

 - Config file or command line (without wallets)  
[config.json.zip](https://github.com/xmrig/xmrig/files/7250124/config.json.zip)

 - OS: Mac OS X and Windows


