Cut any large shallots in half. Melt the butter in a heatproof 20 cm (8 inch) frying pan, add the shallots and cook over a medium heat for 5 minutes until just beginning to colour. Add the sugar and cook for a further 5 minutes or until the shallots are caramelized, turning occasionally so that they cook evenly.
Stir in the vinegar, the leaves from the thyme sprigs and salt and pepper to taste and cook for 2 minutes.
Leave the shallots to cool for 20 minutes in the pan if it has a heatproof handle; if not, transfer to a heavy-based 20 cm (8 inch) round greased cake tin.
Roll out the pastry on a lightly floured surface and trim to a 20 cm (8 inch) circle. Arrange on top of the onions and tuck down the sides of the frying pan or cake tin.
Bake in a preheated oven, 200°C (400°F), Gas Mark 6, for 25–30 minutes until the pastry is well risen and golden. Leave to stand for 5 minutes, then loosen the edges with a knife. Cover with a serving plate or chopping board, invert the pan or cake tin on to the plate and then remove. Serve warm, cut into wedges, with a green leaf salad.