When you are using flex-wrap: wrap
you are telling the container that it can wrap its elements for as long at it wants regardless of the container height.
The container can not hold more than 2 of the blocks you have created so it will always overflow since it can't decide on if it wraps or shrinks.
You can allow the child to shrink and grow by using flex-grow
or flex-shrink
on the child.