params.vertical_total_mid_frame_num = adjust.v_total_mid_frame_num;
 
        for (i = 0; i < num_pipes; i++) {
-               if ((pipe_ctx[i]->stream_res.tg != NULL) && pipe_ctx[i]->stream_res.tg->funcs) {
+               /* dc_state_destruct() might null the stream resources, so fetch tg
+                * here first to avoid a race condition. The lifetime of the pointee
+                * itself (the timing_generator object) is not a problem here.
+                */
+               struct timing_generator *tg = pipe_ctx[i]->stream_res.tg;
+
+               if ((tg != NULL) && tg->funcs) {
                        if (pipe_ctx[i]->stream && pipe_ctx[i]->stream->ctx->dc->debug.static_screen_wait_frames) {
                                struct dc_crtc_timing *timing = &pipe_ctx[i]->stream->timing;
                                struct dc *dc = pipe_ctx[i]->stream->ctx->dc;
                                        num_frames = 2 * (frame_rate % 60);
                                }
                        }
-                       if (pipe_ctx[i]->stream_res.tg->funcs->set_drr)
-                               pipe_ctx[i]->stream_res.tg->funcs->set_drr(
-                                       pipe_ctx[i]->stream_res.tg, ¶ms);
+                       if (tg->funcs->set_drr)
+                               tg->funcs->set_drr(tg, ¶ms);
                        if (adjust.v_total_max != 0 && adjust.v_total_min != 0)
-                               if (pipe_ctx[i]->stream_res.tg->funcs->set_static_screen_control)
-                                       pipe_ctx[i]->stream_res.tg->funcs->set_static_screen_control(
-                                               pipe_ctx[i]->stream_res.tg,
-                                               event_triggers, num_frames);
+                               if (tg->funcs->set_static_screen_control)
+                                       tg->funcs->set_static_screen_control(
+                                               tg, event_triggers, num_frames);
                }
        }
 }