I'm not exactly sure by what you mean by trace filter but I've seen this error in cases where I've done something dumb like adding the ns_db gethandle call in a loop. Or in a long page tried to aquire the handle twice.
I think that the main page/function you are calling the filtering code from already has a handle to the log pool and then the filter function tries to aquire one too.
One solution is to aquire the db handle in the main function/page and pass it into your trace filtering function as an argument.