### Summary
When we generate tokens with the same credentials at high tps (>5…0) we see intermittent HTTP 500 responses with `{"message":"An unexpected error occurred"}` in the payload. In the logs, we see this stacktrace:
```
2018/01/26 21:05:40 [error] 48#0: *83278 [lua] handler.lua:104: [events] missing entity in crud subscriber, client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.mydomain.com"
--
| 2018/01/26 21:05:40 [error] 48#0: *83278 lua coroutine: runtime error: /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:71: attempt to index local 'token' (a nil value)
| stack traceback:
| coroutine 0:
| /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua: in function 'generate_token'
| /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:338: in function 'execute'
| /usr/local/share/lua/5.1/kong/plugins/oauth2/handler.lua:12: in function </usr/local/share/lua/5.1/kong/plugins/oauth2/handler.lua:10>
| coroutine 1:
| [C]: in function 'resume'
| coroutine.wrap:21: in function <coroutine.wrap:21>
| /usr/local/share/lua/5.1/kong/init.lua:374: in function 'access'
| access_by_lua(nginx-kong.conf:92):2: in function <access_by_lua(nginx-kong.conf:92):1>, client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.mydomain.com"
| 2018/01/26 21:05:40 [error] 48#0: *83278 [lua] responses.lua:121: access(): /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:71: attempt to index local 'token' (a nil value), client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.mydomain.com"
```
### Steps To Reproduce
1. Set up a proxy with the OAuth 2.0 Plugin, and Client Credentials enabled
2. Create a new consumer with client credentials
3. At around 50 tps, create tokens at the /oauth2/token endpoint with those credentials
### Additional Details & Logs
- Kong version: 0.12.1
- Kong debug-level startup logs
```
2018/01/26 21:12:30 [debug] 46#0: *2104 [lua] cluster.lua:428: next_coordinator(): [lua-cassandra] load balancing policy chose host at 10.204.90.125
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:20: rewrite(): executing plugin "rate-limiting": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:20: rewrite(): executing plugin "rate-limiting": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:20: rewrite(): executing plugin "kong-upstream-jwt": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:20: rewrite(): executing plugin "custom-kong-http-log-plugin": rewrite
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:20: rewrite(): executing plugin "kong-upstream-jwt": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:20: rewrite(): executing plugin "statsd": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:20: rewrite(): executing plugin "correlation-id": rewrite
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:20: rewrite(): executing plugin "custom-kong-http-log-plugin": rewrite
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:20: rewrite(): executing plugin "statsd": rewrite
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:20: rewrite(): executing plugin "correlation-id": rewrite
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] base_plugin.lua:24: access(): executing plugin "oauth2": access
2018/01/26 21:12:30 [debug] 40#0: *2120 [lua] base_plugin.lua:24: access(): executing plugin "oauth2": access
2018/01/26 21:12:30 [debug] 46#0: *2114 [lua] cluster.lua:428: next_coordinator(): [lua-cassandra] load balancing policy chose host at 10.86.36.142
2018/01/26 21:12:30 [info] 46#0: *2096 client 10.1.49.1 closed keepalive connection (104: Connection reset by peer)
2018/01/26 21:12:30 [error] 46#0: *2113 [lua] handler.lua:104: [events] missing entity in crud subscriber, client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.mydomain.com"
2018/01/26 21:12:30 [error] 46#0: *2113 lua coroutine: runtime error: /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:71: attempt to index local 'token' (a nil value)
stack traceback:
coroutine 0:
/usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua: in function 'generate_token'
/usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:338: in function 'execute'
/usr/local/share/lua/5.1/kong/plugins/oauth2/handler.lua:12: in function </usr/local/share/lua/5.1/kong/plugins/oauth2/handler.lua:10>
coroutine 1:
[C]: in function 'resume'
coroutine.wrap:21: in function <coroutine.wrap:21>
/usr/local/share/lua/5.1/kong/init.lua:374: in function 'access'
access_by_lua(nginx-kong.conf:92):2: in function <access_by_lua(nginx-kong.conf:92):1>, client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.mydomain.com"
2018/01/26 21:12:30 [error] 46#0: *2113 [lua] responses.lua:121: access(): /usr/local/share/lua/5.1/kong/plugins/oauth2/access.lua:71: attempt to index local 'token' (a nil value), client: 10.1.51.1, server: kong, request: "POST /auth/oauth2/token HTTP/1.1", host: "gateway-dev-core-elr.custom.com"
```
- Kong error logs ( )
see above
- Kong configuration (registered APIs/Plugins & configuration file)
API
```
{
"created_at": 1511816567681,
"strip_uri": true,
"id": "e39d742e-3cec-4bdf-b1a1-c8c8cc53cbf8",
"name": "Ross_Test_Proxy",
"upstream_url": "https://google.com",
"http_if_terminated": false,
"https_only": false,
"retries": 5,
"uris": [
"/ross/test"
],
"upstream_send_timeout": 6000,
"upstream_connect_timeout": 6000,
"upstream_read_timeout": 6000,
"methods": {},
"preserve_host": false,
"health_checks": {
"createdUser": null,
"updatedUser": null,
"id": 3,
"api_id": "e39d742e-3cec-4bdf-b1a1-c8c8cc53cbf8",
"api": {
"created_at": 1511816567681,
"strip_uri": true,
"id": "e39d742e-3cec-4bdf-b1a1-c8c8cc53cbf8",
"name": "Ross_Test_Proxy",
"upstream_url": "http://secrethost.com:8000",
"http_if_terminated": false,
"https_only": false,
"retries": 5,
"uris": [
"/ross/test"
],
"upstream_send_timeout": 6000,
"upstream_connect_timeout": 6000,
"upstream_read_timeout": 6000,
"methods": "",
"preserve_host": false
},
"health_check_endpoint": "",
"notification_endpoint": "",
"active": false,
"data": null,
"createdAt": "2017-11-30T05:25:14.000Z",
"updatedAt": "2017-11-30T05:25:14.000Z"
}
}
```
Plugins:
```
{
"total": 3,
"data": [
{
"created_at": 1516723644052,
"config": {
"refresh_token_ttl": 120 9600,
"token_expiration": 7200,
"mandatory_scope": false,
"hide_credentials": false,
"a ccept_http_if_already_terminated": false,
"enable_implicit_grant": false,
"global_cr edentials": true,
"provision_key": "function",
"enable_password_grant": false,
"enable _authorization_code": false,
"anonymous": "",
"enable_client_credentials": true,
"auth _header_name": "authorization"
},
"id": "98e1ac1a-9c34-4c51-b3e9-31c95f1a8c86",
"enab led": false,
"name": "oauth2",
"api_id": "e39d742e-3cec-4bdf-b1a1-c8c8cc53cbf8"
},
{
"cr eated_at": 1516824832470,
"config": {},
"id": "f0b76aff-d7f7-4cbd-9df4-199e8b8b2870",
"enabled": true,
"name": "stdout-log",
"api_id": "e39d742e-3cec-4bdf-b1a1-c8c8cc53cbf 8"
},
{
"created_at": 1516723790418,
"config": {
"whitelist": [
"ross_oauth_group"
]
},
"id": "5d12721f-46d8-49f8-882c-094cee9ac935",
"enabled": false,
"name": "acl",
"api_id": "e 39d742e-3cec-4bdf-b1a1-c8c8cc53cbf8"
}
]
}
```
Kong Configurations
```
KONG_LOG_LEVEL | debug
KONG_PROXY_ACCESS_LOG | off
KONG_ADMIN_ACCESS_LOG | off
KONG_PROXY_ERROR_LOG | /dev/stderr
KONG_ADMIN_ERROR_LOG | /dev/stderr
KONG_ANONYMOUS_REPORTS | off
KONG_PROXY_LISTEN | 0.0.0.0:8000
KONG_PROXY_LISTEN_SSL | 0.0.0.0:8443
KONG_ADMIN_LISTEN | 0.0.0.0:8001
KONG_ADMIN_LISTEN_SSL | 0.0.0.0:8444
KONG_MEM_CACHE_SIZE | 256m
KONG_SSL_CERT | /usr/local/kong/ssl/kongcert.crt
KONG_SSL_CERT_KEY | /usr/local/kong/ssl/kongprivatekey.key
KONG_HTTP2 | on
KONG_UPSTREAM_KEEPALIVE | 60
KONG_SERVER_TOKENS | off
KONG_LATENCY_TOKENS | on
KONG_REAL_IP_HEADER | X-Real-IP
KONG_REAL_IP_RECURSIVE | off
KONG_CLIENT_MAX_BODY_SIZE | 0
KONG_CLIENT_BODY_BUFFER_SIZE | 5m
KONG_ERROR_DEFAULT_TYPE | text/plain
KONG_DATABASE | cassandra
KONG_CASSANDRA_PORT | 9042
KONG_CASSANDRA_KEYSPACE | kong
KONG_CASSANDRA_TIMEOUT | 5000
KONG_CASSANDRA_SSL | on
KONG_CASSANDRA_SSL_VERIFY | on
KONG_CASSANDRA_USERNAME | ...
KONG_CASSANDRA_PASSWORD | ...
KONG_CASSANDRA_CONSISTENCY | ONE
KONG_CASSANDRA_LB_POLICY | RoundRobin
KONG_DB_UPDATE_FREQUENCY | 5
KONG_DB_UPDATE_PROPAGATION | 5
KONG_DB_CACHE_TTL | 10800
KONG_DNS_STALE_TTL | 4
KONG_DNS_NOT_FOUND_TTL | 30
KONG_DNS_ERROR_TTL | 1
KONG_DNS_NO_SYNC | off
KONG_LUA_SSL_TRUSTED_CERTIFICATE | /usr/local/kong/ssl/kongcert.pem
KONG_LUA_SSL_VERIFY_DEPTH | 2
KONG_LUA_SOCKET_POOL_SIZE | 30
KONG_CASSANDRA_CONTACT_POINTS | 10.86.36.142,10.204.96.124,10.204.96.125,10.204.94.124,10.204.90.125
KONG_DNS_ORDER | LAST,SRV,A,CNAME
KONG_CUSTOM_PLUGINS | kong-upstream-jwt,stdout-log,custom-kong-http-log-plugin
```
- Operating System: Alpine 3.6