fix infinite loop bug when executing query