diff --git a/main/file.c b/main/file.c
index c66742f72e83c25ed65d202f0f9b9778ae5866d5..5855f45227eb3e0b62680beb3d39517306ee72f0 100644
--- a/main/file.c
+++ b/main/file.c
@@ -865,6 +865,23 @@ int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
 
 int ast_closestream(struct ast_filestream *f)
 {
+	/* This used to destroy the filestream, but it now just decrements a refcount.
+	 * We need to force the stream to quit queuing frames now, because we might
+	 * change the writeformat, which could result in a subsequent write error, if
+	 * the format is different. */
+
+	/* Stop a running stream if there is one */
+	if (f->owner) {
+		if (f->fmt->format < AST_FORMAT_AUDIO_MASK) {
+			f->owner->stream = NULL;
+			AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
+			ast_settimeout(f->owner, 0, NULL, NULL);
+		} else {
+			f->owner->vstream = NULL;
+			AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
+		}
+	}
+
 	if (ast_test_flag(&f->fr, AST_FRFLAG_FROM_FILESTREAM)) {
 		/* If this flag is still set, it essentially means that the reference
 		 * count of f is non-zero. We can't destroy this filestream until