When running walletpassphrase on a large wallet.dat (ie about 300,000 segwit P2SH addresses) the command sometimes takes a very long time, and if command is run multiple times at once (while running slow) it causes bitcoin core to freeze (stops syncing and requires kill -9 to stop/restart)

During the locked up walletpassphrase commands being run the following messages start to appear in the debug.log:

`018-01-24 01:01:04 socket sending timeout: 1201s`

Note that this problem is intermittent, so sometimes the command runs instantly, other times it takes a few minutes, and other times it completely freezes the node.
* @return true if succeeded, false if failed due to empty keypool
     * @throws std::runtime_error if keypool read failed, key was invalid,
     *     was not found in the wallet, or was misclassified in the internal
     *     or external keypool
     */
    bool ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fRequestedInternal);
public:
    using ScriptPubKeyMan::ScriptPubKeyMan;
    bool GetNewDestination(const OutputType type, CTxDestination& dest, std::string& error) override;
    isminetype IsMine(const CScript& script) const override;
    bool CheckDecryptionKey(const CKeyingMaterial& master_key, bool accept_no_keys = false) override;
    bool Encrypt(const CKeyingMaterial& master_key, WalletBatch* batch) override;
    bool GetReservedDestination(const OutputType type, bool internal, CTxDestination& address, int64_t& index, CKeyPool& keypool) override;
    void KeepDestination(int64_t index, const OutputType& type) override;
    void ReturnDestination(int64_t index, bool internal, const CTxDestination&) override;
    bool TopUp(unsigned int size = 0) override;
    void MarkUnusedAddresses(const CScript& script) override;
    
    void UpgradeKeyMetadata();
    bool IsHDEnabled() const override;
    bool SetupGeneration(bool force = false) override;
    bool Upgrade(int prev_version, bilingual_str& error) override;
    bool HavePrivateKeys() const override;
    void RewriteDB() override;
    int64_t GetOldestKeyPoolTime() const override;
    size_t KeypoolCountExternalKeys() const override;
    unsigned int GetKeyPoolSize() const override;
    int64_t GetTimeFirstKey() const override;
    std::unique_ptr<CKeyMetadata> GetMetadata(const CTxDestination& dest) const override;
    bool CanGetAddresses(bool internal = false) const override;