Merge pull request #346 from shenrie/master

Adding generic prepare resource support
Flush agent state where set can happen in a non RPC thread.
Better debugging