An in situ formed poly(vinylene carbonate)–lithium iodide gel electrolyte enables stable cycling of lithium metal and a thermal shutdown function.