@@ -175,6 +175,70 @@ func TestCollectorReportError(t *testing.T) {
175175 assert .Equal (t , StateClosed , col .GetState ())
176176}
177177
178+ func TestCollectorShutdownWithFatalError (t * testing.T ) {
179+ factories , err := nopFactories ()
180+ require .NoError (t , err )
181+
182+ factory := newFatalOnShutdownExtensionFactory ()
183+ factories .Extensions [factory .Type ()] = factory
184+
185+ col , err := NewCollector (CollectorSettings {
186+ BuildInfo : component .NewDefaultBuildInfo (),
187+ Factories : func () (Factories , error ) { return factories , nil },
188+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-fatalerror.yaml" )}),
189+ })
190+ require .NoError (t , err )
191+
192+ wg := startCollector (context .Background (), t , col )
193+
194+ assert .Eventually (t , func () bool {
195+ return StateRunning == col .GetState ()
196+ }, 2 * time .Second , 200 * time .Millisecond )
197+
198+ col .Shutdown ()
199+
200+ done := make (chan struct {})
201+ go func () {
202+ wg .Wait ()
203+ close (done )
204+ }()
205+
206+ select {
207+ case <- done :
208+ assert .Equal (t , StateClosed , col .GetState ())
209+ case <- time .After (10 * time .Second ):
210+ t .Fatal ("collector shutdown deadlocked" )
211+ }
212+ }
213+
214+ type fatalOnShutdownExtension struct {
215+ component.StartFunc
216+ host component.Host
217+ }
218+
219+ func (e * fatalOnShutdownExtension ) Start (_ context.Context , host component.Host ) error {
220+ e .host = host
221+ return nil
222+ }
223+
224+ func (e * fatalOnShutdownExtension ) Shutdown (context.Context ) error {
225+ componentstatus .ReportStatus (e .host , componentstatus .NewFatalErrorEvent (errors .New ("shutdown error" )))
226+ return nil
227+ }
228+
229+ func newFatalOnShutdownExtensionFactory () extension.Factory {
230+ return extension .NewFactory (
231+ component .MustNewType ("fatalonshutdown" ),
232+ func () component.Config {
233+ return & struct {}{}
234+ },
235+ func (context.Context , extension.Settings , component.Config ) (extension.Extension , error ) {
236+ return & fatalOnShutdownExtension {}, nil
237+ },
238+ component .StabilityLevelStable ,
239+ )
240+ }
241+
178242// NewStatusWatcherExtensionFactory returns a component.ExtensionFactory to construct a status watcher extension.
179243func NewStatusWatcherExtensionFactory (
180244 onStatusChanged func (source * componentstatus.InstanceID , event * componentstatus.Event ),
0 commit comments