# API disabled in the context of log\_by\_lua, Unable to perform database action as well tcp connection in log(), header\_filter(), body\_filter() modules in my custom plugin

**URL:** <https://discuss.konghq.com/t/api-disabled-in-the-context-of-log-by-lua-unable-to-perform-database-action-as-well-tcp-connection-in-log-header-filter-body-filter-modules-in-my-custom-plugin/4497>\
**Category:** General\
**Created:** [September 26, 2019, 5:20am UTC](https://discuss.konghq.com/t/api-disabled-in-the-context-of-log-by-lua-unable-to-perform-database-action-as-well-tcp-connection-in-log-header-filter-body-filter-modules-in-my-custom-plugin/4497 "2019-09-26T05:20:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishanthkumarv94](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nishanthkumarv94](https://discuss.konghq.com/u/nishanthkumarv94)\
**Post date:** [September 26, 2019, 5:20am UTC](https://discuss.konghq.com/t/api-disabled-in-the-context-of-log-by-lua-unable-to-perform-database-action-as-well-tcp-connection-in-log-header-filter-body-filter-modules-in-my-custom-plugin/4497/1 "2019-09-26T05:20:49Z")

</div>

Hi,  
I am building a custom plugin where in I am facing an issue during the database operation like insert, delete, select etc., as well as in tcp connection when it is invoked through : **log** (), **header\_filter** , **body\_filter** (). Below is the the error message.

[error] 6634#0: _67 failed to run log\_by\_lua_: /usr/local/share/lua/5.1/kong/globalpatches.lua:321: **API disabled in the context of log\_by\_lua** \*  
stack traceback:  
**[C]: in function 'old\_tcp’**  
/usr/local/share/lua/5.1/kong/globalpatches.lua:321: in function ‘tcp’  
/usr/local/share/lua/5.1/kong/plugins/ethercis/access.lua:54: in function ‘login\_ethercis’  
/usr/local/share/lua/5.1/kong/plugins/ethercis/handler.lua:22: in function ‘log’  
/usr/local/share/lua/5.1/kong/init.lua:529: in function ‘log’  
log\_by\_lua(nginx-kong.conf:124):2: in function \<log\_by\_lua(nginx-kong.conf:124):1\> while logging request, client: 127.0.0.1, server: kong, req

anyone please give some alternative implementation to perform database action only in log() or header\_filter() or body\_filter modules in a plugin.

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![rucciva](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/rucciva/32/1391_2.png) [@rucciva](https://discuss.konghq.com/u/rucciva)\
**Post date:** [September 26, 2019, 9:09am UTC](https://discuss.konghq.com/t/api-disabled-in-the-context-of-log-by-lua-unable-to-perform-database-action-as-well-tcp-connection-in-log-header-filter-body-filter-modules-in-my-custom-plugin/4497/2 "2019-09-26T09:09:59Z")

</div>

Hi, per [https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#cosockets-not-available-everywhere](https://github.com/openresty/lua-nginx-module/blob/master/README.markdown#cosockets-not-available-everywhere), it’s not possible to do what you want.

The only workaround is to use ngx.timer.at , but you won’t be able to wait for the operation to complete. It’s kinda fire and forget mechanism

---

<div class="post-metadata">

**Author:** ![nishanthkumarv94](https://avatars.discourse-cdn.com/v4/letter/n/eb9ed0/32.png) [@nishanthkumarv94](https://discuss.konghq.com/u/nishanthkumarv94)\
**Post date:** [September 26, 2019, 9:12am UTC](https://discuss.konghq.com/t/api-disabled-in-the-context-of-log-by-lua-unable-to-perform-database-action-as-well-tcp-connection-in-log-header-filter-body-filter-modules-in-my-custom-plugin/4497/3 "2019-09-26T09:12:58Z")

</div>

Thank you for the information @rucciva
