pwolanin pointed out that taxonomy_access_set_term_grants() leaves a risk for race conditions, because it deletes all database records for the given conditions before inserting new ones. It would be better to use a transaction anyway, since an incomplete set of changes could result in broken content access, depending on the particular access rules.