diff -Nurp net-snmp-5.4.2.1-orig/agent/mibgroup/agentx/master_admin.c net-snmp-5.4.2.1/agent/mibgroup/agentx/master_admin.c
--- net-snmp-5.4.2.1-orig/agent/mibgroup/agentx/master_admin.c	2015-12-16 10:18:25.954463779 +0000
+++ net-snmp-5.4.2.1/agent/mibgroup/agentx/master_admin.c	2015-12-16 10:23:52.969839757 +0000
@@ -166,6 +166,7 @@ close_agentx_session(netsnmp_session * s
     for (sp = session->subsession; sp != NULL; sp = sp->next) {
 
         if (sp->sessid == sessid) {
+            netsnmp_remove_delegated_requests_for_session(sp);
             unregister_mibs_by_session(sp);
             unregister_index_by_session(sp);
             snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
diff -Nurp net-snmp-5.4.2.1-orig/agent/mibgroup/agentx/master.c net-snmp-5.4.2.1/agent/mibgroup/agentx/master.c
--- net-snmp-5.4.2.1-orig/agent/mibgroup/agentx/master.c	2015-12-16 10:18:25.954463779 +0000
+++ net-snmp-5.4.2.1/agent/mibgroup/agentx/master.c	2015-12-16 10:21:36.686098828 +0000
@@ -236,10 +236,13 @@ agentx_got_response(int operation,
     if (!cache) {
         DEBUGMSGTL(("agentx/master", "response too late on session %08p\n",
                     session));
-        /* response is too late, free the cache */
+        /*
+         * Response is too late, free the cache and return 1
+         * so that the session pending request list item can be deleted
+         */
         if (magic)
             netsnmp_free_delegated_cache((netsnmp_delegated_cache*) magic);
-        return 0;
+        return 1;
     }
     requests = cache->requests;
 
diff -Nurp net-snmp-5.4.2.1-orig/agent/snmp_agent.c net-snmp-5.4.2.1/agent/snmp_agent.c
--- net-snmp-5.4.2.1-orig/agent/snmp_agent.c	2015-12-16 10:18:25.978463733 +0000
+++ net-snmp-5.4.2.1/agent/snmp_agent.c	2015-12-16 10:45:16.999459983 +0000
@@ -1299,6 +1299,7 @@ init_agent_snmp_session(netsnmp_session
     asp->treecache_num = -1;
     asp->treecache_len = 0;
     asp->reqinfo = SNMP_MALLOC_TYPEDEF(netsnmp_agent_request_info);
+    asp->flags = SNMP_AGENT_FLAGS_NONE;
     DEBUGMSGTL(("verbose:asp", "asp %p reqinfo %p created\n",
                 asp, asp->reqinfo));
 
@@ -1351,6 +1352,9 @@ netsnmp_check_for_delegated(netsnmp_agen
 
     if (NULL == asp->treecache)
         return 0;
+
+    if (asp->flags & SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS)
+        return 0;
     
     for (i = 0; i <= asp->treecache_num; i++) {
         for (request = asp->treecache[i].requests_begin; request;
@@ -1429,39 +1433,49 @@ int
 netsnmp_remove_delegated_requests_for_session(netsnmp_session *sess)
 {
     netsnmp_agent_session *asp;
-    int count = 0;
+    int total_count = 0;
     
     for (asp = agent_delegated_list; asp; asp = asp->next) {
         /*
          * check each request
          */
+        int i;
+        int count = 0;
         netsnmp_request_info *request;
-        for(request = asp->requests; request; request = request->next) {
-            /*
-             * check session
-             */
-            netsnmp_assert(NULL!=request->subtree);
-            if(request->subtree->session != sess)
-                continue;
+        for (i = 0; i <= asp->treecache_num; i++) {
+            for (request = asp->treecache[i].requests_begin; request;
+                 request = request->next) {
+                /*
+                 * check session
+                 */
+                netsnmp_assert(NULL!=request->subtree);
+                if(request->subtree->session != sess)
+                    continue;
+
+                /*
+                 * matched! mark request as done
+                 */
+                netsnmp_request_set_error(request, SNMP_ERR_GENERR);
+                ++count;
+            }
+        }
+        if (count) {
+            asp->flags |= SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS;
+            total_count += count;
 
-            /*
-             * matched! mark request as done
-             */
-            netsnmp_request_set_error(request, SNMP_ERR_GENERR);
-            ++count;
         }
     }
 
     /*
      * if we found any, that request may be finished now
      */
-    if(count) {
+    if(total_count) {
         DEBUGMSGTL(("snmp_agent", "removed %d delegated request(s) for session "
-                    "%08p\n", count, sess));
-        netsnmp_check_outstanding_agent_requests();
+                    "%08p\n", total_count, sess));
+        netsnmp_check_delegated_requests();
     }
     
-    return count;
+    return total_count;
 }
 
 int
@@ -2604,19 +2618,11 @@ handle_var_requests(netsnmp_agent_sessio
     return final_status;
 }
 
-/*
- * loop through our sessions known delegated sessions and check to see
- * if they've completed yet. If there are no more delegated sessions,
- * check for and process any queued requests
- */
 void
-netsnmp_check_outstanding_agent_requests(void)
+netsnmp_check_delegated_requests(void)
 {
     netsnmp_agent_session *asp, *prev_asp = NULL, *next_asp = NULL;
 
-    /*
-     * deal with delegated requests
-     */
     for (asp = agent_delegated_list; asp; asp = next_asp) {
         next_asp = asp->next;   /* save in case we clean up asp */
         if (!netsnmp_check_for_delegated(asp)) {
@@ -2655,6 +2661,22 @@ netsnmp_check_outstanding_agent_requests
             prev_asp = asp;
         }
     }
+}
+
+/*
+ * loop through our sessions known delegated sessions and check to see
+ * if they've completed yet. If there are no more delegated sessions,
+ * check for and process any queued requests
+ */
+void
+netsnmp_check_outstanding_agent_requests(void)
+{
+    netsnmp_agent_session *asp;
+
+    /*
+     * deal with delegated requests
+     */
+    netsnmp_check_delegated_requests();
 
     /*
      * if we are processing a set and there are more delegated
@@ -2683,7 +2705,8 @@ netsnmp_check_outstanding_agent_requests
 
             netsnmp_processing_set = netsnmp_agent_queued_list;
             DEBUGMSGTL(("snmp_agent", "SET request remains queued while "
-                        "delegated requests finish, asp = %08p\n", asp));
+                        "delegated requests finish, asp = %8p\n",
+                        agent_delegated_list));
             break;
         }
 
@@ -2743,6 +2766,10 @@ check_delayed_request(netsnmp_agent_sess
     case SNMP_MSG_GETBULK:
     case SNMP_MSG_GETNEXT:
         netsnmp_check_all_requests_status(asp, 0);
+        if (asp->flags & SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS) {
+            DEBUGMSGTL(("snmp_agent","canceling next walk for asp %p\n", asp));
+            break;
+        }
         handle_getnext_loop(asp);
         if (netsnmp_check_for_delegated(asp) &&
             netsnmp_check_transaction_id(asp->pdu->transid) !=
diff -Nurp net-snmp-5.4.2.1-orig/include/net-snmp/agent/snmp_agent.h net-snmp-5.4.2.1/include/net-snmp/agent/snmp_agent.h
--- net-snmp-5.4.2.1-orig/include/net-snmp/agent/snmp_agent.h	2015-12-16 10:18:25.858463963 +0000
+++ net-snmp-5.4.2.1/include/net-snmp/agent/snmp_agent.h	2015-12-16 10:47:09.659255840 +0000
@@ -32,6 +32,9 @@ extern          "C" {
 #define SNMP_MAX_PDU_SIZE 64000 /* local constraint on PDU size sent by agent
                                  * (see also SNMP_MAX_MSG_SIZE in snmp_api.h) */
 
+#define SNMP_AGENT_FLAGS_NONE                   0x0
+#define SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS     0x1
+
     /*
      * If non-zero, causes the addresses of peers to be logged when receptions
      * occur.  
@@ -197,6 +200,7 @@ extern          "C" {
         int             treecache_num;  /* number of current cache entries */
         netsnmp_cachemap *cache_store;
         int             vbcount;
+        int             flags;
     } netsnmp_agent_session;
 
     /*
@@ -232,6 +236,7 @@ extern          "C" {
     int             init_master_agent(void);
     void            shutdown_master_agent(void);
     int             agent_check_and_process(int block);
+    void            netsnmp_check_delegated_requests(void);
     void            netsnmp_check_outstanding_agent_requests(void);
 
     int             netsnmp_request_set_error(netsnmp_request_info *request,
