A regression test I’m writing failed because I was using invalidateblock() and then re-generating another chain.
But I got an identical block hash, so the newly-generated block was immediately rejected. I believe the root cause is the generate RPC call not calling reservekey.keepKey() to remove it from the keypool.
I’m opening this issue in case I missed some discussion when generate was implemented, and this is the intended behavior. Fixing it will slow down regression tests that call generate repeatedly (because new keys will be created instead of the same key being used repeatedly).