diff --git a/main/channel.c b/main/channel.c
index b253cc102dfbca2dff1470e4159b611a87f59af7..6b29fd9ec5bd9ab90999ad554d9d5dab87eb77cd 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -2255,6 +2255,7 @@ static void ast_read_generator_actions(struct ast_channel *chan, struct ast_fram
 {
 	if (chan->generatordata &&  !ast_internal_timing_enabled(chan)) {
 		void *tmp = chan->generatordata;
+		int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
 		int res;
 		int samples;
 
@@ -2273,6 +2274,10 @@ static void ast_read_generator_actions(struct ast_channel *chan, struct ast_fram
 		} else {
 			samples = f->samples;
 		}
+		
+		if (chan->generator->generate) {
+			generate = chan->generator->generate;
+		}
 		/* This unlock is here based on two assumptions that hold true at this point in the
 		 * code. 1) this function is only called from within __ast_read() and 2) all generators
 		 * call ast_write() in their generate callback.
@@ -2282,7 +2287,7 @@ static void ast_read_generator_actions(struct ast_channel *chan, struct ast_fram
 		 * avoidance not to work in deeper functions
 		 */
 		ast_channel_unlock(chan);
-		res = chan->generator->generate(chan, tmp, f->datalen, samples);
+		res = generate(chan, tmp, f->datalen, f->samples);
 		ast_channel_lock(chan);
 		chan->generatordata = tmp;
 		if (res) {