Just as an alternative to the other answer, one way would be to use bash's coproc
facility:
{coproc FOO { foo; } 2>&1 1>&3; } 3>&1
CHILD=$!
while read line <&${FOO[0]}; do
if echo "$line" | grep -q bar; then
kill $CHILD
else
echo "$line"
fi
done
That's clearly bash-specific, though.