as Spain couldn't close the border under EU rules (as you allude to, the EU accepts Gib as British territory).
Spain also accepts Gibraltar as being part of the EU. For instance, Spanish national IDs are valid for travel between EU countries but not between EU and non-EU countries - if you go to the Spain-Gibraltar border almost every Spanish citizen presents an ID card not a passport. A former mayor of La Linea also realised that Spain could not charge people for people to travel over the border, they proposed a 'congestion charge' for vehicles entering La Linea for the purpose of crossing in to Gibraltar in an attempt to find a loophole but the EU said that would still be a border crossing fee in their eyes.